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 = PhoneApp.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,
     69                                     Intent intent) {
     70         if (DBG) log("onActivityResult");
     71 
     72         switch (requestCode) {
     73             case PIN2_REQUEST_CODE:
     74                 Bundle extras = (intent != null) ? intent.getExtras() : null;
     75                 if (extras != null) {
     76                     mPin2 = extras.getString("pin2");
     77                     showStatus(getResources().getText(
     78                             R.string.deleting_fdn_contact));
     79                     deleteContact();
     80                 } else {
     81                     // if they cancelled, then we just cancel too.
     82                     if (DBG) log("onActivityResult: CANCELLED");
     83                     displayProgress(false);
     84                     finish();
     85                 }
     86                 break;
     87         }
     88     }
     89 
     90     private void resolveIntent() {
     91         Intent intent = getIntent();
     92 
     93         mName =  intent.getStringExtra(INTENT_EXTRA_NAME);
     94         mNumber =  intent.getStringExtra(INTENT_EXTRA_NUMBER);
     95 
     96         if (TextUtils.isEmpty(mName)) {
     97             finish();
     98         }
     99     }
    100 
    101     private void deleteContact() {
    102         StringBuilder buf = new StringBuilder();
    103         buf.append("tag='");
    104         buf.append(mName);
    105         buf.append("' AND number='");
    106         buf.append(mNumber);
    107         buf.append("' AND pin2='");
    108         buf.append(mPin2);
    109         buf.append("'");
    110 
    111         Uri uri = Uri.parse("content://icc/fdn");
    112 
    113         mQueryHandler = new QueryHandler(getContentResolver());
    114         mQueryHandler.startDelete(0, null, uri, buf.toString(), null);
    115         displayProgress(true);
    116     }
    117 
    118     private void authenticatePin2() {
    119         Intent intent = new Intent();
    120         intent.setClass(this, GetPin2Screen.class);
    121         startActivityForResult(intent, PIN2_REQUEST_CODE);
    122     }
    123 
    124     private void displayProgress(boolean flag) {
    125         getWindow().setFeatureInt(
    126                 Window.FEATURE_INDETERMINATE_PROGRESS,
    127                 flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
    128     }
    129 
    130     // Replace the status field with a toast to make things appear similar
    131     // to the rest of the settings.  Removed the useless status field.
    132     private void showStatus(CharSequence statusMsg) {
    133         if (statusMsg != null) {
    134             Toast.makeText(this, statusMsg, Toast.LENGTH_SHORT)
    135             .show();
    136         }
    137     }
    138 
    139     private void handleResult(boolean success) {
    140         if (success) {
    141             if (DBG) log("handleResult: success!");
    142             showStatus(getResources().getText(R.string.fdn_contact_deleted));
    143         } else {
    144             if (DBG) log("handleResult: failed!");
    145             showStatus(getResources().getText(R.string.pin2_invalid));
    146         }
    147 
    148         mHandler.postDelayed(new Runnable() {
    149             public void run() {
    150                 finish();
    151             }
    152         }, 2000);
    153 
    154     }
    155 
    156     private class QueryHandler extends AsyncQueryHandler {
    157         public QueryHandler(ContentResolver cr) {
    158             super(cr);
    159         }
    160 
    161         @Override
    162         protected void onQueryComplete(int token, Object cookie, Cursor c) {
    163         }
    164 
    165         protected void onInsertComplete(int token, Object cookie,
    166                                         Uri uri) {
    167         }
    168 
    169         protected void onUpdateComplete(int token, Object cookie, int result) {
    170         }
    171 
    172         protected void onDeleteComplete(int token, Object cookie, int result) {
    173             if (DBG) log("onDeleteComplete");
    174             displayProgress(false);
    175             handleResult(result > 0);
    176         }
    177 
    178     }
    179 
    180     private void log(String msg) {
    181         Log.d(LOG_TAG, "[DeleteFdnContact] " + msg);
    182     }
    183 }
    184