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