• 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 enable USB mass storage
35  * on-demand (that is, when the USB cable is connected). It uses the alert
36  * dialog style. It will be launched from a notification.
37  */
38 public class UsbStorageActivity extends AlertActivity implements DialogInterface.OnClickListener {
39 
40     private static final int POSITIVE_BUTTON = AlertDialog.BUTTON1;
41 
42     /** Used to detect when the USB cable is unplugged, so we can call finish() */
43     private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
44         @Override
45         public void onReceive(Context context, Intent intent) {
46             if (intent.getAction() == Intent.ACTION_BATTERY_CHANGED) {
47                 handleBatteryChanged(intent);
48             }
49         }
50     };
51 
52     @Override
onCreate(Bundle savedInstanceState)53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55 
56         // Set up the "dialog"
57         final AlertController.AlertParams p = mAlertParams;
58         p.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
59         p.mTitle = getString(com.android.internal.R.string.usb_storage_title);
60         p.mMessage = getString(com.android.internal.R.string.usb_storage_message);
61         p.mPositiveButtonText = getString(com.android.internal.R.string.usb_storage_button_mount);
62         p.mPositiveButtonListener = this;
63         p.mNegativeButtonText = getString(com.android.internal.R.string.usb_storage_button_unmount);
64         p.mNegativeButtonListener = this;
65         setupAlert();
66     }
67 
68     @Override
onResume()69     protected void onResume() {
70         super.onResume();
71 
72         registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
73     }
74 
75     @Override
onPause()76     protected void onPause() {
77         super.onPause();
78 
79         unregisterReceiver(mBatteryReceiver);
80     }
81 
82     /**
83      * {@inheritDoc}
84      */
onClick(DialogInterface dialog, int which)85     public void onClick(DialogInterface dialog, int which) {
86 
87         if (which == POSITIVE_BUTTON) {
88             mountAsUsbStorage();
89         }
90 
91         // No matter what, finish the activity
92         finish();
93     }
94 
mountAsUsbStorage()95     private void mountAsUsbStorage() {
96         IMountService mountService = IMountService.Stub.asInterface(ServiceManager
97                 .getService("mount"));
98         if (mountService == null) {
99             showSharingError();
100             return;
101         }
102 
103         try {
104             mountService.setMassStorageEnabled(true);
105         } catch (RemoteException e) {
106             showSharingError();
107             return;
108         }
109     }
110 
handleBatteryChanged(Intent intent)111     private void handleBatteryChanged(Intent intent) {
112         int pluggedType = intent.getIntExtra("plugged", 0);
113         if (pluggedType == 0) {
114             // It was disconnected from the plug, so finish
115             finish();
116         }
117     }
118 
showSharingError()119     private void showSharingError() {
120         Toast.makeText(this, com.android.internal.R.string.usb_storage_error_message,
121                 Toast.LENGTH_LONG).show();
122     }
123 
124 }
125