OSDN Git Service

am 66b5a58a: Merge "Fix init order so we have something to measure." into mnc-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / deviceinfo / PublicVolumeSettings.java
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.settings.deviceinfo;
18
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.os.UserManager;
24 import android.os.storage.DiskInfo;
25 import android.os.storage.StorageEventListener;
26 import android.os.storage.StorageManager;
27 import android.os.storage.VolumeInfo;
28 import android.os.storage.VolumeRecord;
29 import android.preference.Preference;
30 import android.preference.PreferenceScreen;
31 import android.provider.DocumentsContract;
32 import android.text.TextUtils;
33 import android.text.format.Formatter;
34 import android.text.format.Formatter.BytesResult;
35
36 import com.android.internal.logging.MetricsLogger;
37 import com.android.internal.util.Preconditions;
38 import com.android.settings.R;
39 import com.android.settings.SettingsPreferenceFragment;
40 import com.android.settings.deviceinfo.StorageSettings.MountTask;
41 import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
42
43 import java.io.File;
44 import java.util.Objects;
45
46 /**
47  * Panel showing summary and actions for a {@link VolumeInfo#TYPE_PUBLIC}
48  * storage volume.
49  */
50 public class PublicVolumeSettings extends SettingsPreferenceFragment {
51     // TODO: disable unmount when providing over MTP/PTP
52
53     private StorageManager mStorageManager;
54
55     private String mVolumeId;
56     private VolumeInfo mVolume;
57     private DiskInfo mDisk;
58
59     private StorageSummaryPreference mSummary;
60
61     private Preference mMount;
62     private Preference mUnmount;
63     private Preference mFormatPublic;
64     private Preference mFormatPrivate;
65
66     private boolean mIsPermittedToAdopt;
67
68     private boolean isVolumeValid() {
69         return (mVolume != null) && (mVolume.getType() == VolumeInfo.TYPE_PUBLIC)
70                 && mVolume.isMountedReadable();
71     }
72
73     @Override
74     protected int getMetricsCategory() {
75         return MetricsLogger.DEVICEINFO_STORAGE;
76     }
77
78     @Override
79     public void onCreate(Bundle icicle) {
80         super.onCreate(icicle);
81
82         final Context context = getActivity();
83
84         mIsPermittedToAdopt = UserManager.get(context).isAdminUser();
85
86         mStorageManager = context.getSystemService(StorageManager.class);
87
88         if (DocumentsContract.ACTION_DOCUMENT_ROOT_SETTINGS.equals(
89                 getActivity().getIntent().getAction())) {
90             final Uri rootUri = getActivity().getIntent().getData();
91             final String fsUuid = DocumentsContract.getRootId(rootUri);
92             mVolume = mStorageManager.findVolumeByUuid(fsUuid);
93         } else {
94             final String volId = getArguments().getString(VolumeInfo.EXTRA_VOLUME_ID);
95             mVolume = mStorageManager.findVolumeById(volId);
96         }
97
98         if (!isVolumeValid()) {
99             getActivity().finish();
100             return;
101         }
102
103         mDisk = mStorageManager.findDiskById(mVolume.getDiskId());
104         Preconditions.checkNotNull(mDisk);
105
106         mVolumeId = mVolume.getId();
107
108         addPreferencesFromResource(R.xml.device_info_storage_volume);
109         getPreferenceScreen().setOrderingAsAdded(true);
110
111         mSummary = new StorageSummaryPreference(context);
112
113         mMount = buildAction(R.string.storage_menu_mount);
114         mUnmount = buildAction(R.string.storage_menu_unmount);
115         mFormatPublic = buildAction(R.string.storage_menu_format);
116         if (mIsPermittedToAdopt) {
117             mFormatPrivate = buildAction(R.string.storage_menu_format_private);
118         }
119     }
120
121     public void update() {
122         if (!isVolumeValid()) {
123             getActivity().finish();
124             return;
125         }
126
127         getActivity().setTitle(mStorageManager.getBestVolumeDescription(mVolume));
128
129         final Context context = getActivity();
130         final PreferenceScreen screen = getPreferenceScreen();
131
132         screen.removeAll();
133
134         if (mVolume.isMountedReadable()) {
135             addPreference(mSummary);
136
137             final File file = mVolume.getPath();
138             final long totalBytes = file.getTotalSpace();
139             final long freeBytes = file.getFreeSpace();
140             final long usedBytes = totalBytes - freeBytes;
141
142             final BytesResult result = Formatter.formatBytes(getResources(), usedBytes, 0);
143             mSummary.setTitle(TextUtils.expandTemplate(getText(R.string.storage_size_large),
144                     result.value, result.units));
145             mSummary.setSummary(getString(R.string.storage_volume_used,
146                     Formatter.formatFileSize(context, totalBytes)));
147             mSummary.setPercent((int) ((usedBytes * 100) / totalBytes));
148         }
149
150         if (mVolume.getState() == VolumeInfo.STATE_UNMOUNTED) {
151             addPreference(mMount);
152         }
153         if (mVolume.isMountedReadable()) {
154             addPreference(mUnmount);
155         }
156         addPreference(mFormatPublic);
157         if (mDisk.isAdoptable() && mIsPermittedToAdopt) {
158             addPreference(mFormatPrivate);
159         }
160     }
161
162     private void addPreference(Preference pref) {
163         pref.setOrder(Preference.DEFAULT_ORDER);
164         getPreferenceScreen().addPreference(pref);
165     }
166
167     private Preference buildAction(int titleRes) {
168         final Preference pref = new Preference(getActivity());
169         pref.setTitle(titleRes);
170         return pref;
171     }
172
173     @Override
174     public void onResume() {
175         super.onResume();
176
177         // Refresh to verify that we haven't been formatted away
178         mVolume = mStorageManager.findVolumeById(mVolumeId);
179         if (!isVolumeValid()) {
180             getActivity().finish();
181             return;
182         }
183
184         mStorageManager.registerListener(mStorageListener);
185         update();
186     }
187
188     @Override
189     public void onPause() {
190         super.onPause();
191         mStorageManager.unregisterListener(mStorageListener);
192     }
193
194     @Override
195     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference pref) {
196         final Context context = getActivity();
197         if (pref == mMount) {
198             new MountTask(context, mVolume).execute();
199         } else if (pref == mUnmount) {
200             new UnmountTask(context, mVolume).execute();
201         } else if (pref == mFormatPublic) {
202             final Intent intent = new Intent(context, StorageWizardFormatConfirm.class);
203             intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
204             intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, false);
205             startActivity(intent);
206         } else if (pref == mFormatPrivate) {
207             final Intent intent = new Intent(context, StorageWizardFormatConfirm.class);
208             intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
209             intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, true);
210             startActivity(intent);
211         }
212
213         return super.onPreferenceTreeClick(preferenceScreen, pref);
214     }
215
216     private final StorageEventListener mStorageListener = new StorageEventListener() {
217         @Override
218         public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
219             if (Objects.equals(mVolume.getId(), vol.getId())) {
220                 mVolume = vol;
221                 update();
222             }
223         }
224
225         @Override
226         public void onVolumeRecordChanged(VolumeRecord rec) {
227             if (Objects.equals(mVolume.getFsUuid(), rec.getFsUuid())) {
228                 mVolume = mStorageManager.findVolumeById(mVolumeId);
229                 update();
230             }
231         }
232     };
233 }