• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.tv.settings.device.storage;
18 
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.os.storage.StorageManager;
28 import android.os.storage.VolumeInfo;
29 import android.support.annotation.NonNull;
30 import android.support.annotation.Nullable;
31 import android.support.v17.leanback.app.GuidedStepFragment;
32 import android.support.v17.leanback.widget.GuidanceStylist;
33 import android.support.v17.leanback.widget.GuidedAction;
34 import android.support.v4.content.LocalBroadcastManager;
35 import android.text.TextUtils;
36 import android.view.View;
37 import android.widget.Toast;
38 
39 import com.android.tv.settings.R;
40 import com.android.tv.settings.dialog.ProgressDialogFragment;
41 
42 import java.util.List;
43 
44 public class UnmountActivity extends Activity {
45 
46     private static final String TAG = "UnmountActivity";
47 
48     public static final String EXTRA_VOLUME_DESC = "UnmountActivity.volumeDesc";
49 
50     private String mUnmountVolumeId;
51     private String mUnmountVolumeDesc;
52 
53     private final Handler mHandler = new Handler();
54     private final BroadcastReceiver mUnmountReceiver = new UnmountReceiver();
55 
getIntent(Context context, String volumeId, String volumeDesc)56     public static Intent getIntent(Context context, String volumeId, String volumeDesc) {
57         final Intent i = new Intent(context, UnmountActivity.class);
58         i.putExtra(VolumeInfo.EXTRA_VOLUME_ID, volumeId);
59         i.putExtra(EXTRA_VOLUME_DESC, volumeDesc);
60         return i;
61     }
62 
63     @Override
onCreate(@ullable Bundle savedInstanceState)64     protected void onCreate(@Nullable Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66 
67         mUnmountVolumeId = getIntent().getStringExtra(VolumeInfo.EXTRA_VOLUME_ID);
68         mUnmountVolumeDesc = getIntent().getStringExtra(EXTRA_VOLUME_DESC);
69 
70         LocalBroadcastManager.getInstance(this).registerReceiver(mUnmountReceiver,
71                 new IntentFilter(SettingsStorageService.ACTION_UNMOUNT));
72 
73         if (savedInstanceState == null) {
74             final StorageManager storageManager = getSystemService(StorageManager.class);
75             final VolumeInfo volumeInfo = storageManager.findVolumeById(mUnmountVolumeId);
76 
77             if (volumeInfo == null) {
78                 // Unmounted already, just bail
79                 finish();
80                 return;
81             }
82 
83             if (volumeInfo.getType() == VolumeInfo.TYPE_PRIVATE) {
84                 final Fragment fragment = UnmountPrivateStepFragment.newInstance(mUnmountVolumeId);
85                 getFragmentManager().beginTransaction()
86                         .replace(android.R.id.content, fragment)
87                         .commit();
88             } else {
89                 // Jump straight to unmounting
90                 onRequestUnmount();
91             }
92         }
93     }
94 
95     @Override
onDestroy()96     protected void onDestroy() {
97         super.onDestroy();
98 
99         LocalBroadcastManager.getInstance(this).unregisterReceiver(mUnmountReceiver);
100     }
101 
onRequestUnmount()102     public void onRequestUnmount() {
103         final Fragment fragment = UnmountProgressFragment.newInstance(mUnmountVolumeDesc);
104         getFragmentManager().beginTransaction()
105                 .replace(android.R.id.content, fragment)
106                 .commit();
107         // Post this so that it will presumably run after onResume, if we're calling from onCreate()
108         mHandler.post(new Runnable() {
109             @Override
110             public void run() {
111                 SettingsStorageService.unmount(UnmountActivity.this, mUnmountVolumeId);
112             }
113         });
114     }
115 
116     @Override
onResume()117     protected void onResume() {
118         super.onResume();
119         final VolumeInfo volumeInfo =
120                 getSystemService(StorageManager.class).findVolumeById(mUnmountVolumeId);
121 
122         if (volumeInfo == null) {
123             // Unmounted already, just bail
124             finish();
125         }
126     }
127 
128     private class UnmountReceiver extends BroadcastReceiver {
129 
130         @Override
onReceive(Context context, Intent intent)131         public void onReceive(Context context, Intent intent) {
132             if (TextUtils.equals(intent.getAction(), SettingsStorageService.ACTION_UNMOUNT)
133                     && TextUtils.equals(intent.getStringExtra(VolumeInfo.EXTRA_VOLUME_ID),
134                     mUnmountVolumeId)) {
135                 final Boolean success =
136                         intent.getBooleanExtra(SettingsStorageService.EXTRA_SUCCESS, false);
137                 if (success) {
138                     Toast.makeText(UnmountActivity.this,
139                             getString(R.string.storage_unmount_success, mUnmountVolumeDesc),
140                             Toast.LENGTH_SHORT).show();
141                 } else {
142                     Toast.makeText(UnmountActivity.this,
143                             getString(R.string.storage_unmount_failure, mUnmountVolumeDesc),
144                             Toast.LENGTH_SHORT).show();
145                 }
146                 finish();
147             }
148         }
149     }
150 
151     public static class UnmountPrivateStepFragment extends GuidedStepFragment {
152 
153         private static final int ACTION_ID_UNMOUNT = 1;
154 
newInstance(String volumeId)155         public static UnmountPrivateStepFragment newInstance(String volumeId) {
156             final UnmountPrivateStepFragment fragment = new UnmountPrivateStepFragment();
157             final Bundle b = new Bundle(1);
158             b.putString(VolumeInfo.EXTRA_VOLUME_ID, volumeId);
159             fragment.setArguments(b);
160             return fragment;
161         }
162 
163         @Override
164         public @NonNull
onCreateGuidance(Bundle savedInstanceState)165         GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
166             return new GuidanceStylist.Guidance(
167                     getString(R.string.storage_wizard_eject_private_title),
168                     getString(R.string.storage_wizard_eject_private_description), "",
169                     getActivity().getDrawable(R.drawable.ic_storage_132dp));
170         }
171 
172         @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)173         public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
174             actions.add(new GuidedAction.Builder(getContext())
175                     .clickAction(GuidedAction.ACTION_ID_CANCEL)
176                     .build());
177             actions.add(new GuidedAction.Builder(getContext())
178                     .id(ACTION_ID_UNMOUNT)
179                     .title(getString(R.string.storage_eject))
180                     .build());
181         }
182 
183         @Override
onGuidedActionClicked(GuidedAction action)184         public void onGuidedActionClicked(GuidedAction action) {
185             final long id = action.getId();
186 
187             if (id == GuidedAction.ACTION_ID_CANCEL) {
188                 getFragmentManager().popBackStack();
189             } else if (id == ACTION_ID_UNMOUNT) {
190                 ((UnmountActivity) getActivity()).onRequestUnmount();
191             }
192         }
193     }
194 
195     public static class UnmountProgressFragment extends ProgressDialogFragment {
196 
197         private static final String ARG_DESCRIPTION = "description";
198 
newInstance(CharSequence description)199         public static UnmountProgressFragment newInstance(CharSequence description) {
200             final Bundle b = new Bundle(1);
201             b.putCharSequence(ARG_DESCRIPTION, description);
202             final UnmountProgressFragment fragment = new UnmountProgressFragment();
203             fragment.setArguments(b);
204             return fragment;
205         }
206 
207         @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)208         public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
209             super.onViewCreated(view, savedInstanceState);
210             final CharSequence description = getArguments().getCharSequence(ARG_DESCRIPTION);
211             setTitle(getActivity().getString(R.string.storage_wizard_eject_progress_title,
212                     description));
213         }
214     }
215 }
216