blob: e1166f120197c9665c94bfb00685437e9ddb3aec [file] [log] [blame]
Danke Xie22d1f9f2009-08-18 18:28:45 -04001/*
2 * Copyright (C) 2007 Google Inc.
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
17package com.android.internal.app;
18
19import android.app.AlertDialog;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.os.Bundle;
Alexy Shin7adcdec2011-07-18 09:42:55 -070026import android.os.Handler;
27import android.os.Message;
Danke Xie22d1f9f2009-08-18 18:28:45 -040028import android.widget.Toast;
29import android.util.Log;
30import android.location.LocationManager;
Hakan Gustavssondea74b02010-11-22 16:08:35 +010031
32import com.android.internal.R;
Danke Xie22d1f9f2009-08-18 18:28:45 -040033import com.android.internal.location.GpsNetInitiatedHandler;
34
35/**
36 * This activity is shown to the user for him/her to accept or deny network-initiated
37 * requests. It uses the alert dialog style. It will be launched from a notification.
38 */
39public class NetInitiatedActivity extends AlertActivity implements DialogInterface.OnClickListener {
40
41 private static final String TAG = "NetInitiatedActivity";
42
43 private static final boolean DEBUG = true;
44 private static final boolean VERBOSE = false;
45
Christian Mehlmauer746a95a2010-05-17 21:16:20 +020046 private static final int POSITIVE_BUTTON = AlertDialog.BUTTON_POSITIVE;
47 private static final int NEGATIVE_BUTTON = AlertDialog.BUTTON_NEGATIVE;
Danke Xie22d1f9f2009-08-18 18:28:45 -040048
Alexy Shin7adcdec2011-07-18 09:42:55 -070049 private static final int GPS_NO_RESPONSE_TIME_OUT = 1;
Danke Xie22d1f9f2009-08-18 18:28:45 -040050 // Received ID from intent, -1 when no notification is in progress
51 private int notificationId = -1;
Alexy Shin7adcdec2011-07-18 09:42:55 -070052 private int timeout = -1;
53 private int default_response = -1;
54 private int default_response_timeout = 6;
Danke Xie22d1f9f2009-08-18 18:28:45 -040055
56 /** Used to detect when NI request is received */
57 private BroadcastReceiver mNetInitiatedReceiver = new BroadcastReceiver() {
58 @Override
59 public void onReceive(Context context, Intent intent) {
60 if (DEBUG) Log.d(TAG, "NetInitiatedReceiver onReceive: " + intent.getAction());
61 if (intent.getAction() == GpsNetInitiatedHandler.ACTION_NI_VERIFY) {
62 handleNIVerify(intent);
63 }
64 }
65 };
66
Alexy Shin7adcdec2011-07-18 09:42:55 -070067 private final Handler mHandler = new Handler() {
68 public void handleMessage(Message msg) {
69 switch (msg.what) {
70 case GPS_NO_RESPONSE_TIME_OUT: {
71 if (notificationId != -1) {
72 sendUserResponse(default_response);
73 }
74 finish();
75 }
76 break;
77 default:
78 }
79 }
80 };
81
Danke Xie22d1f9f2009-08-18 18:28:45 -040082 @Override
83 protected void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
85
86 // Set up the "dialog"
87 final Intent intent = getIntent();
88 final AlertController.AlertParams p = mAlertParams;
Hakan Gustavssondea74b02010-11-22 16:08:35 +010089 Context context = getApplicationContext();
Danke Xie22d1f9f2009-08-18 18:28:45 -040090 p.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
91 p.mTitle = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TITLE);
92 p.mMessage = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_MESSAGE);
Hakan Gustavssondea74b02010-11-22 16:08:35 +010093 p.mPositiveButtonText = String.format(context.getString(R.string.gpsVerifYes));
Danke Xie22d1f9f2009-08-18 18:28:45 -040094 p.mPositiveButtonListener = this;
Hakan Gustavssondea74b02010-11-22 16:08:35 +010095 p.mNegativeButtonText = String.format(context.getString(R.string.gpsVerifNo));
Danke Xie22d1f9f2009-08-18 18:28:45 -040096 p.mNegativeButtonListener = this;
97
98 notificationId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
Alexy Shin7adcdec2011-07-18 09:42:55 -070099 timeout = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TIMEOUT, default_response_timeout);
100 default_response = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_DEFAULT_RESPONSE, GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT);
101 if (DEBUG) Log.d(TAG, "onCreate() : notificationId: " + notificationId + " timeout: " + timeout + " default_response:" + default_response);
Danke Xie22d1f9f2009-08-18 18:28:45 -0400102
Alexy Shin7adcdec2011-07-18 09:42:55 -0700103 mHandler.sendMessageDelayed(mHandler.obtainMessage(GPS_NO_RESPONSE_TIME_OUT), (timeout * 1000));
Danke Xie22d1f9f2009-08-18 18:28:45 -0400104 setupAlert();
105 }
106
107 @Override
108 protected void onResume() {
109 super.onResume();
110 if (DEBUG) Log.d(TAG, "onResume");
111 registerReceiver(mNetInitiatedReceiver, new IntentFilter(GpsNetInitiatedHandler.ACTION_NI_VERIFY));
112 }
113
114 @Override
115 protected void onPause() {
116 super.onPause();
117 if (DEBUG) Log.d(TAG, "onPause");
118 unregisterReceiver(mNetInitiatedReceiver);
119 }
120
121 /**
122 * {@inheritDoc}
123 */
124 public void onClick(DialogInterface dialog, int which) {
125 if (which == POSITIVE_BUTTON) {
126 sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT);
127 }
128 if (which == NEGATIVE_BUTTON) {
129 sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_DENY);
130 }
131
132 // No matter what, finish the activity
133 finish();
134 notificationId = -1;
135 }
136
137 // Respond to NI Handler under GpsLocationProvider, 1 = accept, 2 = deny
138 private void sendUserResponse(int response) {
139 if (DEBUG) Log.d(TAG, "sendUserResponse, response: " + response);
140 LocationManager locationManager = (LocationManager)
141 this.getSystemService(Context.LOCATION_SERVICE);
142 locationManager.sendNiResponse(notificationId, response);
143 }
144
145 private void handleNIVerify(Intent intent) {
146 int notifId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
147 notificationId = notifId;
148
149 if (DEBUG) Log.d(TAG, "handleNIVerify action: " + intent.getAction());
150 }
151
152 private void showNIError() {
153 Toast.makeText(this, "NI error" /* com.android.internal.R.string.usb_storage_error_message */,
154 Toast.LENGTH_LONG).show();
155 }
156}