Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2006 The Android Open Source 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.android.phone;
     18 
     19 import android.app.Activity;
     20 import android.content.AsyncQueryHandler;
     21 import android.content.ContentResolver;
     22 import android.content.Intent;
     23 import android.database.Cursor;
     24 import android.net.Uri;
     25 import android.os.Bundle;
     26 import android.os.Handler;
     27 import android.text.TextUtils;
     28 import android.util.Log;
     29 import android.view.Window;
     30 import android.widget.Toast;
     31 
     32 import static android.view.Window.PROGRESS_VISIBILITY_OFF;
     33 import static android.view.Window.PROGRESS_VISIBILITY_ON;
     34 
     35 /**
     36  * Activity to let the user delete an FDN contact.
     37  */
     38 public class DeleteFdnContactScreen extends Activity {
     39     private static final String LOG_TAG = PhoneGlobals.LOG_TAG;
     40     private static final boolean DBG = false;
     41 
     42     private static final String INTENT_EXTRA_NAME = "name";
     43     private static final String INTENT_EXTRA_NUMBER = "number";
     44 
     45     private static final int PIN2_REQUEST_CODE = 100;
     46 
     47     private String mName;
     48     private String mNumber;
     49     private String mPin2;
     50 
     51     protected QueryHandler mQueryHandler;
     52 
     53     private Handler mHandler = new Handler();
     54 
     55     @Override
     56     protected void onCreate(Bundle icicle) {
     57         super.onCreate(icicle);
     58 
     59         resolveIntent();
     60 
     61         authenticatePin2();
     62 
     63         getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
     64         setContentView(R.layout.delete_fdn_contact_screen);
     65     }
     66 
     67     @Override
     68     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
     69         if (DBG) log("onActivityResult");
     70 
     71         switch (requestCode) {
     72             case PIN2_REQUEST_CODE:
     73                 Bundle extras = (intent != null) ? intent.getExtras() : null;
     74                 if (extras != null) {
     75                     mPin2 = extras.getString("pin2");
     76                     showStatus(getResources().getText(
     77                             R.string.deleting_fdn_contact));
     78                     deleteContact();
     79                 } else {
     80                     // if they cancelled, then we just cancel too.
     81                     if (DBG) log("onActivityResult: CANCELLED");
     82                     displayProgress(false);
     83                     finish();
     84                 }
     85                 break;
     86         }
     87     }
     88 
     89     private void resolveIntent() {
     90         Intent intent = getIntent();
     91 
     92         mName =  intent.getStringExtra(INTENT_EXTRA_NAME);
     93         mNumber =  intent.getStringExtra(INTENT_EXTRA_NUMBER);
     94 
     95         if (TextUtils.isEmpty(mNumber)) {
     96             finish();
     97         }
     98     }
     99 
    100     private void deleteContact() {
    101         StringBuilder buf = new StringBuilder();
    102         if (TextUtils.isEmpty(mName)) {
    103             buf.append("number='");
    104         } else {
    105             buf.append("tag='");
    106             buf.append(mName);
    107             buf.append("' AND number='");
    108         }
    109         buf.append(mNumber);
    110         buf.append("' AND pin2='");
    111         buf.append(mPin2);
    112         buf.append("'");
    113 
    114         Uri uri = Uri.parse("content://icc/fdn");
    115 
    116         mQueryHandler = new QueryHandler(getContentResolver());
    117         mQueryHandler.startDelete(0, null, uri, buf.toString(), null);
    118         displayProgress(true);
    119     }
    120 
    121     private void authenticatePin2() {
    122         Intent intent = new Intent();
    123         intent.setClass(this, GetPin2Screen.class);
    124         startActivityForResult(intent, PIN2_REQUEST_CODE);
    125     }
    126 
    127     private void displayProgress(boolean flag) {
    128         getWindow().setFeatureInt(
    129                 Window.FEATURE_INDETERMINATE_PROGRESS,
    130                 flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
    131     }
    132 
    133     // Replace the status field with a toast to make things appear similar
    134     // to the rest of the settings.  Removed the useless status field.
    135     private void showStatus(CharSequence statusMsg) {
    136         if (statusMsg != null) {
    137             Toast.makeText(this, statusMsg, Toast.LENGTH_SHORT)
    138             .show();
    139         }
    140     }
    141 
    142     private void handleResult(boolean success) {
    143         if (success) {
    144             if (DBG) log("handleResult: success!");
    145             showStatus(getResources().getText(R.string.fdn_contact_deleted));
    146         } else {
    147             if (DBG) log("handleResult: failed!");
    148             showStatus(getResources().getText(R.string.pin2_invalid));
    149         }
    150 
    151         mHandler.postDelayed(new Runnable() {
    152             @Override
    153             public void run() {
    154                 finish();
    155             }
    156         }, 2000);
    157 
    158     }
    159 
    160     private class QueryHandler extends AsyncQueryHandler {
    161         public QueryHandler(ContentResolver cr) {
    162             super(cr);
    163         }
    164 
    165         @Override
    166         protected void onQueryComplete(int token, Object cookie, Cursor c) {
    167         }
    168 
    169         @Override
    170         protected void onInsertComplete(int token, Object cookie, Uri uri) {
    171         }
    172 
    173         @Override
    174         protected void onUpdateComplete(int token, Object cookie, int result) {
    175         }
    176 
    177         @Override
    178         protected void onDeleteComplete(int token, Object cookie, int result) {
    179             if (DBG) log("onDeleteComplete");
    180             displayProgress(false);
    181             handleResult(result > 0);
    182         }
    183 
    184     }
    185 
    186     private void log(String msg) {
    187         Log.d(LOG_TAG, "[DeleteFdnContact] " + msg);
    188     }
    189 }
    190