blob: 5787d620add767cb45649452db3768036329089b [file] [log] [blame]
Chiao Chengd80c4342012-12-03 17:15:58 -08001/*
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 */
Gary Mai69c182a2016-12-05 13:07:03 -080016package com.android.contacts.vcard;
Chiao Chengd80c4342012-12-03 17:15:58 -080017
Brian Attwellc2e912c2014-10-27 12:29:44 -070018import android.app.Activity;
Chiao Chengd80c4342012-12-03 17:15:58 -080019import android.app.AlertDialog;
20import android.app.Dialog;
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.ServiceConnection;
26import android.net.Uri;
27import android.os.Bundle;
28import android.os.IBinder;
29import android.util.Log;
30
Arthur Wang3f6a2442016-12-05 14:51:59 -080031import com.android.contacts.R;
Chiao Chengd80c4342012-12-03 17:15:58 -080032
33/**
34 * The Activity for canceling vCard import/export.
35 */
Brian Attwellc2e912c2014-10-27 12:29:44 -070036public class CancelActivity extends Activity implements ServiceConnection {
Chiao Chengd80c4342012-12-03 17:15:58 -080037 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
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
59 public void onClick(DialogInterface dialog, int which) {
60 finish();
61 }
62 @Override
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
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
84 protected Dialog onCreateDialog(int id, Bundle bundle) {
Sailesh Nepalb4a522e2016-02-20 21:17:32 -080085 if (id == R.id.dialog_cancel_confirmation) {
Chiao Chengd80c4342012-12-03 17:15:58 -080086 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(android.R.string.ok, new RequestCancelListener())
95 .setOnCancelListener(mCancelListener)
96 .setNegativeButton(android.R.string.cancel, mCancelListener);
97 return builder.create();
Sailesh Nepalb4a522e2016-02-20 21:17:32 -080098 } else if (id == R.id.dialog_cancel_failed) {
Chiao Chengd80c4342012-12-03 17:15:58 -080099 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();
Sailesh Nepalb4a522e2016-02-20 21:17:32 -0800106 } else {
Chiao Chengd80c4342012-12-03 17:15:58 -0800107 Log.w(LOG_TAG, "Unknown dialog id: " + id);
Sailesh Nepalb4a522e2016-02-20 21:17:32 -0800108 return super.onCreateDialog(id, bundle);
Chiao Chengd80c4342012-12-03 17:15:58 -0800109 }
Chiao Chengd80c4342012-12-03 17:15:58 -0800110 }
111
112 @Override
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
127 public void onServiceDisconnected(ComponentName name) {
128 // do nothing
129 }
130}