OSDN Git Service

f72a05a99987810902cb973f4af7a702dd123d06
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / ui / dialogs / FilesystemInfoDialog.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod 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.cyanogenmod.filemanager.ui.dialogs;
18
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.widget.Switch;
27 import android.widget.TextView;
28
29 import com.cyanogenmod.filemanager.FileManagerApplication;
30 import com.cyanogenmod.filemanager.R;
31 import com.cyanogenmod.filemanager.console.ConsoleBuilder;
32 import com.cyanogenmod.filemanager.model.DiskUsage;
33 import com.cyanogenmod.filemanager.model.MountPoint;
34 import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
35 import com.cyanogenmod.filemanager.preferences.Preferences;
36 import com.cyanogenmod.filemanager.ui.widgets.DiskUsageGraph;
37 import com.cyanogenmod.filemanager.util.CommandHelper;
38 import com.cyanogenmod.filemanager.util.DialogHelper;
39 import com.cyanogenmod.filemanager.util.FileHelper;
40 import com.cyanogenmod.filemanager.util.MountPointHelper;
41
42 /**
43  * A class that wraps a dialog for showing information about a mount point.<br />
44  * This class display information like mount point name, device name, size, type, ...
45  */
46 public class FilesystemInfoDialog implements OnClickListener {
47
48     /**
49      * An interface to communicate when the user change the mount state
50      * of a filesystem.
51      */
52     public interface OnMountListener {
53         /**
54          * Method invoked when the mount state of a mount point has changed.
55          *
56          * @param mountPoint The mount point that has changed
57          */
58         void onRemount(MountPoint mountPoint);
59     }
60
61
62
63     private static final String TAG = "FilesystemInfoDialog"; //$NON-NLS-1$
64
65     private final MountPoint mMountPoint;
66     /**
67      * @hide
68      */
69     final DiskUsage mDiskUsage;
70
71     private final Context mContext;
72     private final AlertDialog mDialog;
73     private View mInfoViewTab;
74     private View mDiskUsageViewTab;
75     private View mInfoView;
76     private View mDiskUsageView;
77     private Switch mSwStatus;
78     /**
79      * @hide
80      */
81     DiskUsageGraph mDiskUsageGraph;
82     private TextView mInfoMsgView;
83
84     private OnMountListener mOnMountListener;
85
86     private boolean mIsMountAllowed;
87     private final boolean mIsAdvancedMode;
88
89     /**
90      * Constructor of <code>FilesystemInfoDialog</code>.
91      *
92      * @param context The current context
93      * @param mountPoint The mount point information
94      * @param diskUsage The disk usage of the mount point
95      */
96     public FilesystemInfoDialog(Context context, MountPoint mountPoint, DiskUsage diskUsage) {
97         super();
98
99         //Save the context
100         this.mContext = context;
101
102         //Save data
103         this.mMountPoint = mountPoint;
104         this.mDiskUsage = diskUsage;
105         this.mIsMountAllowed = false;
106         this.mIsAdvancedMode = FileManagerApplication.isAdvancedMode();
107
108         //Inflate the content
109         LayoutInflater li =
110                 (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
111         View contentView = li.inflate(R.layout.filesystem_info_dialog, null);
112
113         //Create the dialog
114         this.mDialog = DialogHelper.createDialog(
115                                         context,
116                                         R.drawable.ic_holo_light_sdcard,
117                                         R.string.filesystem_info_dialog_title,
118                                         contentView);
119         this.mDialog.setButton(
120                 DialogInterface.BUTTON_NEGATIVE,
121                 this.mContext.getString(android.R.string.cancel),
122                 (DialogInterface.OnClickListener)null);
123
124         //Fill the dialog
125         fillData(contentView);
126     }
127
128     /**
129      * Method that shows the dialog.
130      */
131     public void show() {
132         this.mDialog.show();
133     }
134
135     /**
136      * Method that sets the listener for listen mount events.
137      *
138      * @param onMountListener The mount listener
139      */
140     public void setOnMountListener(OnMountListener onMountListener) {
141         this.mOnMountListener = onMountListener;
142     }
143
144     /**
145      * Method that fill the dialog with the data of the mount point.
146      *
147      * @param contentView The content view
148      */
149     @SuppressWarnings({ "boxing" })
150     private void fillData(View contentView) {
151         //Get the tab views
152         this.mInfoViewTab = contentView.findViewById(R.id.filesystem_info_dialog_tab_info);
153         this.mDiskUsageViewTab =
154                 contentView.findViewById(R.id.filesystem_info_dialog_tab_disk_usage);
155         this.mInfoView = contentView.findViewById(R.id.filesystem_tab_info);
156         this.mDiskUsageView = contentView.findViewById(R.id.filesystem_tab_diskusage);
157         this.mDiskUsageGraph =
158                 (DiskUsageGraph)contentView.findViewById(R.id.filesystem_disk_usage_graph);
159
160         // Set the user preference about free disk space warning level
161         String fds = Preferences.getSharedPreferences().getString(
162                 FileManagerSettings.SETTINGS_DISK_USAGE_WARNING_LEVEL.getId(),
163                 (String)FileManagerSettings.
164                     SETTINGS_DISK_USAGE_WARNING_LEVEL.getDefaultValue());
165         this.mDiskUsageGraph.setFreeDiskSpaceWarningLevel(Integer.parseInt(fds));
166
167         //Register the listeners
168         this.mInfoViewTab.setOnClickListener(this);
169         this.mDiskUsageViewTab.setOnClickListener(this);
170
171         //Gets text views
172         this.mSwStatus = (Switch)contentView.findViewById(R.id.filesystem_info_status);
173         this.mSwStatus.setOnClickListener(this);
174         TextView tvMountPoint =
175                 (TextView)contentView.findViewById(R.id.filesystem_info_mount_point);
176         TextView tvDevice = (TextView)contentView.findViewById(R.id.filesystem_info_device);
177         TextView tvType = (TextView)contentView.findViewById(R.id.filesystem_info_type);
178         TextView tvOptions = (TextView)contentView.findViewById(R.id.filesystem_info_options);
179         TextView tvDumpPass = (TextView)contentView.findViewById(R.id.filesystem_info_dump_pass);
180         TextView tvTotal =
181                 (TextView)contentView.findViewById(R.id.filesystem_info_total_disk_usage);
182         TextView tvUsed = (TextView)contentView.findViewById(R.id.filesystem_info_used_disk_usage);
183         TextView tvFree = (TextView)contentView.findViewById(R.id.filesystem_info_free_disk_usage);
184         this.mInfoMsgView = (TextView)contentView.findViewById(R.id.filesystem_info_msg);
185
186         //Fill the text views
187         tvMountPoint.setText(this.mMountPoint.getMountPoint());
188         tvDevice.setText(this.mMountPoint.getDevice());
189         tvType.setText(this.mMountPoint.getType());
190         tvOptions.setText(this.mMountPoint.getOptions());
191         tvDumpPass.setText(
192                 String.format("%d / %d",  //$NON-NLS-1$
193                         this.mMountPoint.getDump(),
194                         this.mMountPoint.getPass()));
195         if (this.mDiskUsage != null) {
196             tvTotal.setText(FileHelper.getHumanReadableSize(this.mDiskUsage.getTotal()));
197             tvUsed.setText(FileHelper.getHumanReadableSize(this.mDiskUsage.getUsed()));
198             tvFree.setText(FileHelper.getHumanReadableSize(this.mDiskUsage.getFree()));
199         } else {
200             tvTotal.setText("-"); //$NON-NLS-1$
201             tvUsed.setText("-"); //$NON-NLS-1$
202             tvFree.setText("-"); //$NON-NLS-1$
203         }
204
205         //Configure status switch
206         boolean hasPrivileged = false;
207         try {
208             hasPrivileged = ConsoleBuilder.isPrivileged();
209         } catch (Throwable ex) {/**NON BLOCK**/}
210         boolean mountAllowed =
211                 MountPointHelper.isMountAllowed(this.mMountPoint);
212         if (this.mIsAdvancedMode) {
213             if (hasPrivileged) {
214                 if (!mountAllowed) {
215                     this.mInfoMsgView.setText(
216                             this.mContext.getString(
217                                     R.string.filesystem_info_cant_be_mounted_msg));
218                     this.mInfoMsgView.setVisibility(View.VISIBLE);
219                 }
220             } else {
221                 this.mInfoMsgView.setVisibility(View.VISIBLE);
222                 this.mInfoMsgView.setOnClickListener(this);
223             }
224         } else {
225             mountAllowed = false;
226             this.mInfoMsgView.setVisibility(View.GONE);
227             this.mInfoMsgView.setOnClickListener(null);
228         }
229         this.mIsMountAllowed = hasPrivileged && mountAllowed && this.mIsAdvancedMode;
230         this.mSwStatus.setEnabled(this.mIsMountAllowed);
231         this.mSwStatus.setChecked(MountPointHelper.isReadWrite(this.mMountPoint));
232
233         //Change the tab
234         onClick(this.mInfoViewTab);
235     }
236
237     /**
238      * {@inheritDoc}
239      */
240     @Override
241     public void onClick(View v) {
242         switch (v.getId()) {
243             case R.id.filesystem_info_dialog_tab_info:
244                 if (!this.mInfoViewTab.isSelected()) {
245                     this.mInfoViewTab.setSelected(true);
246                     ((TextView)this.mInfoViewTab).setTextAppearance(
247                             this.mContext, R.style.primary_text_appearance);
248                     this.mDiskUsageViewTab.setSelected(false);
249                     ((TextView)this.mDiskUsageViewTab).setTextAppearance(
250                             this.mContext, R.style.secondary_text_appearance);
251                     this.mInfoView.setVisibility(View.VISIBLE);
252                     this.mDiskUsageView.setVisibility(View.GONE);
253                 }
254                 this.mInfoMsgView.setVisibility(
255                         this.mIsMountAllowed || !this.mIsAdvancedMode ? View.GONE : View.VISIBLE);
256                 break;
257
258             case R.id.filesystem_info_dialog_tab_disk_usage:
259                 if (!this.mDiskUsageViewTab.isSelected()) {
260                     this.mInfoViewTab.setSelected(false);
261                     ((TextView)this.mInfoViewTab).setTextAppearance(
262                             this.mContext, R.style.secondary_text_appearance);
263                     this.mDiskUsageViewTab.setSelected(true);
264                     ((TextView)this.mDiskUsageViewTab).setTextAppearance(
265                             this.mContext, R.style.primary_text_appearance);
266                     this.mInfoView.setVisibility(View.GONE);
267                     this.mDiskUsageView.setVisibility(View.VISIBLE);
268                 }
269                 this.mDiskUsageGraph.post(new Runnable() {
270                     @Override
271                     public void run() {
272                         //Animate disk usage graph
273                         FilesystemInfoDialog.this.mDiskUsageGraph.drawDiskUsage(
274                                 FilesystemInfoDialog.this.mDiskUsage);
275                     }
276                 });
277                 break;
278
279             case R.id.filesystem_info_status:
280                 //Mount the filesystem
281                 Switch sw = (Switch)v;
282                 boolean ret = false;
283                 try {
284                     ret = CommandHelper.remount(
285                             this.mContext,
286                             this.mMountPoint, sw.isChecked(), null);
287                     //Hide warning message
288                     this.mInfoMsgView.setVisibility(View.GONE);
289                     //Communicate the mount change
290                     if (this.mOnMountListener != null) {
291                         this.mOnMountListener.onRemount(this.mMountPoint);
292                     }
293
294                 } catch (Throwable e) {
295                     Log.e(TAG,
296                             String.format(
297                                     "Fail to remount %s", //$NON-NLS-1$
298                                     this.mMountPoint.getMountPoint()), e);
299                 }
300                 if (!ret) {
301                     //Show warning message
302                     this.mInfoMsgView.setText(R.string.filesystem_info_mount_failed_msg);
303                     this.mInfoMsgView.setVisibility(View.VISIBLE);
304                     sw.setChecked(!sw.isChecked());
305                 }
306                 break;
307
308             case R.id.filesystem_info_msg:
309                 //Change the console
310                 boolean superuser = ConsoleBuilder.changeToPrivilegedConsole(this.mContext);
311                 if (superuser) {
312                     this.mInfoMsgView.setOnClickListener(null);
313
314                     // Is filesystem able to be mounted?
315                     boolean mountAllowed = MountPointHelper.isMountAllowed(this.mMountPoint);
316                     if (mountAllowed) {
317                         this.mInfoMsgView.setVisibility(View.GONE);
318                         this.mInfoMsgView.setBackground(null);
319                         this.mSwStatus.setEnabled(true);
320                         this.mIsMountAllowed = true;
321                         break;
322                     }
323
324                     // Show the message
325                     this.mInfoMsgView.setText(
326                             this.mContext.getString(
327                                     R.string.filesystem_info_cant_be_mounted_msg));
328                     this.mInfoMsgView.setVisibility(View.VISIBLE);
329                     this.mIsMountAllowed = false;
330                 }
331                 break;
332
333             default:
334                 break;
335         }
336     }
337
338 }