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
17package com.android.tv.settings.device.storage;
18
19import android.app.Activity;
20import android.app.Fragment;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.storage.StorageManager;
28import android.os.storage.VolumeInfo;
29import android.support.annotation.NonNull;
30import android.support.annotation.Nullable;
31import android.support.v17.leanback.app.GuidedStepFragment;
32import android.support.v17.leanback.widget.GuidanceStylist;
33import android.support.v17.leanback.widget.GuidedAction;
34import android.support.v4.content.LocalBroadcastManager;
35import android.text.TextUtils;
36import android.view.View;
37import android.widget.Toast;
38
39import com.android.tv.settings.R;
40import com.android.tv.settings.dialog.ProgressDialogFragment;
41
42import java.util.List;
43
44public 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
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
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
96    protected void onDestroy() {
97        super.onDestroy();
98
99        LocalBroadcastManager.getInstance(this).unregisterReceiver(mUnmountReceiver);
100    }
101
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
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
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
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
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
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
184        public void onGuidedActionClicked(GuidedAction action) {
185            final long id = action.getId();
186
187            if (id == GuidedAction.ACTION_ID_CANCEL) {
188                if (!getFragmentManager().popBackStackImmediate()) {
189                    getActivity().finish();
190                }
191            } else if (id == ACTION_ID_UNMOUNT) {
192                ((UnmountActivity) getActivity()).onRequestUnmount();
193            }
194        }
195    }
196
197    public static class UnmountProgressFragment extends ProgressDialogFragment {
198
199        private static final String ARG_DESCRIPTION = "description";
200
201        public static UnmountProgressFragment newInstance(CharSequence description) {
202            final Bundle b = new Bundle(1);
203            b.putCharSequence(ARG_DESCRIPTION, description);
204            final UnmountProgressFragment fragment = new UnmountProgressFragment();
205            fragment.setArguments(b);
206            return fragment;
207        }
208
209        @Override
210        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
211            super.onViewCreated(view, savedInstanceState);
212            final CharSequence description = getArguments().getCharSequence(ARG_DESCRIPTION);
213            setTitle(getActivity().getString(R.string.storage_wizard_eject_progress_title,
214                    description));
215        }
216    }
217}
218