• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 package com.android.contacts.vcard;
17 
18 import android.app.Activity;
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.ServiceConnection;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.IBinder;
29 import android.util.Log;
30 
31 import com.android.contacts.R;
32 
33 /**
34  * The Activity for canceling vCard import/export.
35  */
36 public class CancelActivity extends Activity implements ServiceConnection {
37     private final String LOG_TAG = "VCardCancel";
38 
39     /* package */ final static String JOB_ID = "job_id";
40     /* package */ final static String DISPLAY_NAME = "display_name";
41 
42     /**
43      * Type of the process to be canceled. Only used for choosing appropriate title/message.
44      * Must be {@link VCardService#TYPE_IMPORT} or {@link VCardService#TYPE_EXPORT}.
45      */
46     /* package */ final static String TYPE = "type";
47 
48     private class RequestCancelListener implements DialogInterface.OnClickListener {
49         @Override
onClick(DialogInterface dialog, int which)50         public void onClick(DialogInterface dialog, int which) {
51             bindService(new Intent(CancelActivity.this,
52                     VCardService.class), CancelActivity.this, Context.BIND_AUTO_CREATE);
53         }
54     }
55 
56     private class CancelListener
57             implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
58         @Override
onClick(DialogInterface dialog, int which)59         public void onClick(DialogInterface dialog, int which) {
60             finish();
61         }
62         @Override
onCancel(DialogInterface dialog)63         public void onCancel(DialogInterface dialog) {
64             finish();
65         }
66     }
67 
68     private final CancelListener mCancelListener = new CancelListener();
69     private int mJobId;
70     private String mDisplayName;
71     private int mType;
72 
73     @Override
onCreate(Bundle savedInstanceState)74     public void onCreate(Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76         final Uri uri = getIntent().getData();
77         mJobId = Integer.parseInt(uri.getQueryParameter(JOB_ID));
78         mDisplayName = uri.getQueryParameter(DISPLAY_NAME);
79         mType = Integer.parseInt(uri.getQueryParameter(TYPE));
80         showDialog(R.id.dialog_cancel_confirmation);
81     }
82 
83     @Override
onCreateDialog(int id, Bundle bundle)84     protected Dialog onCreateDialog(int id, Bundle bundle) {
85         if (id == R.id.dialog_cancel_confirmation) {
86             final String message;
87             if (mType == VCardService.TYPE_IMPORT) {
88                 message = getString(R.string.cancel_import_confirmation_message, mDisplayName);
89             } else {
90                 message = getString(R.string.cancel_export_confirmation_message, mDisplayName);
91             }
92             final AlertDialog.Builder builder = new AlertDialog.Builder(this)
93                     .setMessage(message)
94                     .setPositiveButton(R.string.yes_button, new RequestCancelListener())
95                     .setOnCancelListener(mCancelListener)
96                     .setNegativeButton(R.string.no_button, mCancelListener);
97             return builder.create();
98         } else if (id == R.id.dialog_cancel_failed) {
99             final AlertDialog.Builder builder = new AlertDialog.Builder(this)
100                     .setTitle(R.string.cancel_vcard_import_or_export_failed)
101                     .setIconAttribute(android.R.attr.alertDialogIcon)
102                     .setMessage(getString(R.string.fail_reason_unknown))
103                     .setOnCancelListener(mCancelListener)
104                     .setPositiveButton(android.R.string.ok, mCancelListener);
105             return builder.create();
106         } else {
107             Log.w(LOG_TAG, "Unknown dialog id: " + id);
108             return super.onCreateDialog(id, bundle);
109         }
110     }
111 
112     @Override
onServiceConnected(ComponentName name, IBinder binder)113     public void onServiceConnected(ComponentName name, IBinder binder) {
114         VCardService service = ((VCardService.MyBinder) binder).getService();
115 
116         try {
117             final CancelRequest request = new CancelRequest(mJobId, mDisplayName);
118             service.handleCancelRequest(request, null);
119         } finally {
120             unbindService(this);
121         }
122 
123         finish();
124     }
125 
126     @Override
onServiceDisconnected(ComponentName name)127     public void onServiceDisconnected(ComponentName name) {
128         // do nothing
129     }
130 }
131