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 com.android.internal.os.storage.ExternalStorageFormatter;
20 
21 import android.app.AlertDialog;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.os.Bundle;
28 import android.util.Log;
29 
30 /**
31  * This activity is shown to the user to confirm formatting of external media.
32  * It uses the alert dialog style. It will be launched from a notification, or from settings
33  */
34 public class ExternalMediaFormatActivity extends AlertActivity implements DialogInterface.OnClickListener {
35 
36     private static final int POSITIVE_BUTTON = AlertDialog.BUTTON_POSITIVE;
37 
38     /** Used to detect when the media state changes, in case we need to call finish() */
39     private BroadcastReceiver mStorageReceiver = new BroadcastReceiver() {
40         @Override
41         public void onReceive(Context context, Intent intent) {
42             String action = intent.getAction();
43             Log.d("ExternalMediaFormatActivity", "got action " + action);
44 
45             if (action == Intent.ACTION_MEDIA_REMOVED ||
46                 action == Intent.ACTION_MEDIA_CHECKING ||
47                 action == Intent.ACTION_MEDIA_MOUNTED ||
48                 action == Intent.ACTION_MEDIA_SHARED) {
49                 finish();
50             }
51         }
52     };
53 
54     @Override
onCreate(Bundle savedInstanceState)55     protected void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57 
58         Log.d("ExternalMediaFormatActivity", "onCreate!");
59         // Set up the "dialog"
60         final AlertController.AlertParams p = mAlertParams;
61         p.mTitle = getString(com.android.internal.R.string.extmedia_format_title);
62         p.mMessage = getString(com.android.internal.R.string.extmedia_format_message);
63         p.mPositiveButtonText = getString(com.android.internal.R.string.extmedia_format_button_format);
64         p.mPositiveButtonListener = this;
65         p.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
66         p.mNegativeButtonListener = this;
67         setupAlert();
68     }
69 
70     @Override
onResume()71     protected void onResume() {
72         super.onResume();
73 
74         IntentFilter filter = new IntentFilter();
75         filter.addAction(Intent.ACTION_MEDIA_REMOVED);
76         filter.addAction(Intent.ACTION_MEDIA_CHECKING);
77         filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
78         filter.addAction(Intent.ACTION_MEDIA_SHARED);
79         registerReceiver(mStorageReceiver, filter);
80     }
81 
82     @Override
onPause()83     protected void onPause() {
84         super.onPause();
85 
86         unregisterReceiver(mStorageReceiver);
87     }
88 
89     /**
90      * {@inheritDoc}
91      */
onClick(DialogInterface dialog, int which)92     public void onClick(DialogInterface dialog, int which) {
93 
94         if (which == POSITIVE_BUTTON) {
95             Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY);
96             intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
97             startService(intent);
98         }
99 
100         // No matter what, finish the activity
101         finish();
102     }
103 }
104