summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/device/storage/NewStorageActivity.java
blob: a39c4f1a2a6c3604d4a2fb37b23426960d88d419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.storage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.storage.DiskInfo;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.os.storage.VolumeRecord;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.leanback.app.GuidedStepSupportFragment;
import androidx.leanback.widget.GuidanceStylist;
import androidx.leanback.widget.GuidedAction;

import com.android.tv.settings.R;
import com.android.tv.settings.device.StorageResetActivity;

import java.util.List;

public class NewStorageActivity extends FragmentActivity {

    private static final String TAG = "NewStorageActivity";

    private static final String ACTION_NEW_STORAGE =
            "com.android.tv.settings.device.storage.NewStorageActivity.NEW_STORAGE";
    private static final String ACTION_MISSING_STORAGE =
            "com.android.tv.settings.device.storage.NewStorageActivity.MISSING_STORAGE";

    public static Intent getNewStorageLaunchIntent(Context context, String volumeId,
            String diskId) {
        final Intent i = new Intent(context, NewStorageActivity.class);
        i.setAction(ACTION_NEW_STORAGE);
        i.putExtra(VolumeInfo.EXTRA_VOLUME_ID, volumeId);
        i.putExtra(DiskInfo.EXTRA_DISK_ID, diskId);
        return i;
    }

    public static Intent getMissingStorageLaunchIntent(Context context, String fsUuid) {
        final Intent i = new Intent(context, NewStorageActivity.class);
        i.setAction(ACTION_MISSING_STORAGE);
        i.putExtra(VolumeRecord.EXTRA_FS_UUID, fsUuid);
        return i;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            final String action = getIntent().getAction();

            if (TextUtils.equals(action, ACTION_MISSING_STORAGE)) {
                // Launched from a notification, see com.android.systemui.usb.StorageNotification
                final String fsUuid = getIntent().getStringExtra(VolumeRecord.EXTRA_FS_UUID);
                if (TextUtils.isEmpty(fsUuid)) {
                    throw new IllegalStateException(
                            "NewStorageActivity launched without specifying missing storage");
                }

                getSupportFragmentManager().beginTransaction()
                        .add(android.R.id.content, MissingStorageFragment.newInstance(fsUuid))
                        .commit();
            } else {
                final String volumeId = getIntent().getStringExtra(VolumeInfo.EXTRA_VOLUME_ID);
                final String diskId = getIntent().getStringExtra(DiskInfo.EXTRA_DISK_ID);
                if (TextUtils.isEmpty(volumeId) && TextUtils.isEmpty(diskId)) {
                    throw new IllegalStateException(
                            "NewStorageActivity launched without specifying new storage");
                }

                getSupportFragmentManager().beginTransaction()
                        .add(android.R.id.content, NewStorageFragment.newInstance(volumeId, diskId))
                        .commit();
            }
        }
    }

    public static class NewStorageFragment extends GuidedStepSupportFragment {

        private static final int ACTION_BROWSE = 1;
        private static final int ACTION_FORMAT_AS_PRIVATE = 2;
        private static final int ACTION_UNMOUNT = 3;
        private static final int ACTION_FORMAT_AS_PUBLIC = 4;

        private String mVolumeId;
        private String mDiskId;
        private String mDescription;

        private final StorageEventListener mStorageEventListener = new StorageEventListener() {
            @Override
            public void onDiskDestroyed(DiskInfo disk) {
                checkForUnmount();
            }

            @Override
            public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
                checkForUnmount();
            }
        };

        public static NewStorageFragment newInstance(String volumeId, String diskId) {
            final Bundle b = new Bundle(1);
            b.putString(VolumeInfo.EXTRA_VOLUME_ID, volumeId);
            b.putString(DiskInfo.EXTRA_DISK_ID, diskId);
            final NewStorageFragment fragment = new NewStorageFragment();
            fragment.setArguments(b);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            StorageManager storageManager = getActivity().getSystemService(StorageManager.class);
            mVolumeId = getArguments().getString(VolumeInfo.EXTRA_VOLUME_ID);
            mDiskId = getArguments().getString(DiskInfo.EXTRA_DISK_ID);
            if (TextUtils.isEmpty(mVolumeId) && TextUtils.isEmpty(mDiskId)) {
                throw new IllegalStateException(
                        "NewStorageFragment launched without specifying new storage");
            }
            if (!TextUtils.isEmpty(mVolumeId)) {
                final VolumeInfo info = storageManager.findVolumeById(mVolumeId);
                mDescription = storageManager.getBestVolumeDescription(info);
                mDiskId = info.getDiskId();
            } else {
                final DiskInfo info = storageManager.findDiskById(mDiskId);
                mDescription = info.getDescription();
            }

            super.onCreate(savedInstanceState);
        }

        @Override
        public void onStart() {
            super.onStart();
            checkForUnmount();
            getActivity().getSystemService(StorageManager.class)
                    .registerListener(mStorageEventListener);
        }

        @Override
        public void onStop() {
            super.onStop();
            getActivity().getSystemService(StorageManager.class)
                    .unregisterListener(mStorageEventListener);
        }

        @Override
        public @NonNull GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
            return new GuidanceStylist.Guidance(
                    getString(R.string.storage_new_title),
                    mDescription,
                    null,
                    getActivity().getDrawable(R.drawable.ic_storage_132dp));
        }

        @Override
        public void onCreateActions(@NonNull List<GuidedAction> actions,
                Bundle savedInstanceState) {
            if (TextUtils.isEmpty(mVolumeId)) {
                actions.add(new GuidedAction.Builder(getContext())
                        .title(R.string.storage_new_action_format_public)
                        .id(ACTION_FORMAT_AS_PUBLIC)
                        .build());
            } else {
                actions.add(new GuidedAction.Builder(getContext())
                        .title(R.string.storage_new_action_browse)
                        .id(ACTION_BROWSE)
                        .build());
            }
            actions.add(new GuidedAction.Builder(getContext())
                    .title(R.string.storage_new_action_adopt)
                    .id(ACTION_FORMAT_AS_PRIVATE)
                    .build());
            actions.add(new GuidedAction.Builder(getContext())
                    .title(R.string.storage_new_action_eject)
                    .id(ACTION_UNMOUNT)
                    .build());
        }

        @Override
        public void onGuidedActionClicked(GuidedAction action) {
            switch ((int) action.getId()) {
                case ACTION_FORMAT_AS_PUBLIC:
                    startActivity(FormatActivity.getFormatAsPublicIntent(getActivity(), mDiskId));
                    break;
                case ACTION_BROWSE:
                    startActivity(new Intent(getActivity(), StorageResetActivity.class));
                    break;
                case ACTION_FORMAT_AS_PRIVATE:
                    startActivity(FormatActivity.getFormatAsPrivateIntent(getActivity(), mDiskId));
                    break;
                case ACTION_UNMOUNT:
                    // If we've mounted a volume, eject it. Otherwise just treat eject as cancel
                    if (!TextUtils.isEmpty(mVolumeId)) {
                        startActivity(
                                UnmountActivity.getIntent(getActivity(), mVolumeId, mDescription));
                    }
                    break;
            }
            getActivity().finish();
        }

        private void checkForUnmount() {
            if (!isAdded()) {
                return;
            }

            final StorageManager storageManager =
                    getContext().getSystemService(StorageManager.class);

            if (!TextUtils.isEmpty(mDiskId)) {
                // If the disk disappears, assume we're done
                final List<DiskInfo> diskInfos = storageManager.getDisks();
                boolean found = false;
                for (DiskInfo diskInfo : diskInfos) {
                    if (TextUtils.equals(diskInfo.getId(), mDiskId)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    getActivity().finish();
                }
            } else if (!TextUtils.isEmpty(mVolumeId)) {
                final List<VolumeInfo> volumeInfos = storageManager.getVolumes();
                boolean found = false;
                for (VolumeInfo volumeInfo : volumeInfos) {
                    if (TextUtils.equals(volumeInfo.getId(), mVolumeId)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    getActivity().finish();
                }
            }
        }
    }

    public static class MissingStorageFragment extends GuidedStepSupportFragment {

        private String mFsUuid;
        private String mDescription;

        private final BroadcastReceiver mDiskReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (TextUtils.equals(intent.getAction(), VolumeInfo.ACTION_VOLUME_STATE_CHANGED)) {
                    checkForRemount();
                }
            }
        };

        public static MissingStorageFragment newInstance(String fsUuid) {
            final MissingStorageFragment fragment = new MissingStorageFragment();
            final Bundle b = new Bundle(1);
            b.putString(VolumeRecord.EXTRA_FS_UUID, fsUuid);
            fragment.setArguments(b);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            StorageManager storageManager = getActivity().getSystemService(StorageManager.class);
            mFsUuid = getArguments().getString(VolumeRecord.EXTRA_FS_UUID);
            if (TextUtils.isEmpty(mFsUuid)) {
                throw new IllegalStateException(
                        "MissingStorageFragment launched without specifying missing storage");
            }
            final VolumeRecord volumeRecord = storageManager.findRecordByUuid(mFsUuid);
            mDescription = volumeRecord.getNickname();

            super.onCreate(savedInstanceState);
        }

        @Override
        public void onStart() {
            super.onStart();
            getContext().registerReceiver(mDiskReceiver,
                    new IntentFilter(VolumeInfo.ACTION_VOLUME_STATE_CHANGED));
            checkForRemount();
        }

        @Override
        public void onStop() {
            super.onStop();
            getContext().unregisterReceiver(mDiskReceiver);
        }

        @Override
        public @NonNull GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
            return new GuidanceStylist.Guidance(
                    getString(R.string.storage_missing_title, mDescription),
                    getString(R.string.storage_missing_description),
                    null,
                    getActivity().getDrawable(R.drawable.ic_error_132dp));
        }

        @Override
        public void onCreateActions(@NonNull List<GuidedAction> actions,
                Bundle savedInstanceState) {
            actions.add(new GuidedAction.Builder(getContext())
                    .clickAction(GuidedAction.ACTION_ID_OK)
                    .build());
        }

        @Override
        public void onGuidedActionClicked(GuidedAction action) {
            getActivity().finish();
        }

        private void checkForRemount() {
            if (!isAdded()) {
                return;
            }

            final List<VolumeInfo> volumeInfos =
                    getContext().getSystemService(StorageManager.class).getVolumes();

            for (final VolumeInfo info : volumeInfos) {
                if (!TextUtils.equals(info.getFsUuid(), mFsUuid)) {
                    continue;
                }
                if (info.isMountedReadable()) {
                    getActivity().finish();
                }
            }
        }
    }

}