• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google Inc.
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.internal.app;
18 
19 import android.app.AlertDialog;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.os.IMountService;
28 import android.os.Message;
29 import android.os.RemoteException;
30 import android.os.ServiceManager;
31 import android.widget.Toast;
32 
33 /**
34  * This activity is shown to the user for him/her to disable USB mass storage.
35  * It uses the alert dialog style. It will be launched from a notification.
36  */
37 public class UsbStorageStopActivity extends AlertActivity implements DialogInterface.OnClickListener {
38 
39     private static final int POSITIVE_BUTTON = AlertDialog.BUTTON1;
40 
41     /** Used to detect when the USB cable is unplugged, so we can call finish() */
42     private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
43         @Override
44         public void onReceive(Context context, Intent intent) {
45             if (intent.getAction() == Intent.ACTION_BATTERY_CHANGED) {
46                 handleBatteryChanged(intent);
47             }
48         }
49     };
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54 
55         // Set up the "dialog"
56         final AlertController.AlertParams p = mAlertParams;
57         p.mIconId = com.android.internal.R.drawable.ic_dialog_alert;
58         p.mTitle = getString(com.android.internal.R.string.usb_storage_stop_title);
59         p.mMessage = getString(com.android.internal.R.string.usb_storage_stop_message);
60         p.mPositiveButtonText = getString(com.android.internal.R.string.usb_storage_stop_button_mount);
61         p.mPositiveButtonListener = this;
62         p.mNegativeButtonText = getString(com.android.internal.R.string.usb_storage_stop_button_unmount);
63         p.mNegativeButtonListener = this;
64         setupAlert();
65     }
66 
67     @Override
onResume()68     protected void onResume() {
69         super.onResume();
70 
71         registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
72     }
73 
74     @Override
onPause()75     protected void onPause() {
76         super.onPause();
77 
78         unregisterReceiver(mBatteryReceiver);
79     }
80 
81     /**
82      * {@inheritDoc}
83      */
onClick(DialogInterface dialog, int which)84     public void onClick(DialogInterface dialog, int which) {
85 
86         if (which == POSITIVE_BUTTON) {
87             stopUsbStorage();
88         }
89 
90         // No matter what, finish the activity
91         finish();
92     }
93 
stopUsbStorage()94     private void stopUsbStorage() {
95         IMountService mountService = IMountService.Stub.asInterface(ServiceManager
96                 .getService("mount"));
97         if (mountService == null) {
98             showStoppingError();
99             return;
100         }
101 
102         try {
103             mountService.setMassStorageEnabled(false);
104         } catch (RemoteException e) {
105             showStoppingError();
106             return;
107         }
108     }
109 
handleBatteryChanged(Intent intent)110     private void handleBatteryChanged(Intent intent) {
111         int pluggedType = intent.getIntExtra("plugged", 0);
112         if (pluggedType == 0) {
113             // It was disconnected from the plug, so finish
114             finish();
115         }
116     }
117 
showStoppingError()118     private void showStoppingError() {
119         Toast.makeText(this, com.android.internal.R.string.usb_storage_stop_error_message,
120                 Toast.LENGTH_LONG).show();
121     }
122 
123 }
124