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