NetInitiatedActivity.java revision ffc3f8f40c7eff0bc3bb43f74a1a0e9235acb258
1/* 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; 26import android.os.Handler; 27import android.os.Message; 28import android.os.RemoteException; 29import android.os.ServiceManager; 30import android.widget.Toast; 31import android.util.Log; 32import android.location.LocationManager; 33import 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 46 private static final int POSITIVE_BUTTON = AlertDialog.BUTTON1; 47 private static final int NEGATIVE_BUTTON = AlertDialog.BUTTON2; 48 49 // Dialog button text 50 public static final String BUTTON_TEXT_ACCEPT = "Accept"; 51 public static final String BUTTON_TEXT_DENY = "Deny"; 52 53 // Received ID from intent, -1 when no notification is in progress 54 private int notificationId = -1; 55 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 67 @Override 68 protected void onCreate(Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 71 // Set up the "dialog" 72 final Intent intent = getIntent(); 73 final AlertController.AlertParams p = mAlertParams; 74 p.mIconId = com.android.internal.R.drawable.ic_dialog_usb; 75 p.mTitle = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TITLE); 76 p.mMessage = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_MESSAGE); 77 p.mPositiveButtonText = BUTTON_TEXT_ACCEPT; 78 p.mPositiveButtonListener = this; 79 p.mNegativeButtonText = BUTTON_TEXT_DENY; 80 p.mNegativeButtonListener = this; 81 82 notificationId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1); 83 if (DEBUG) Log.d(TAG, "onCreate, notifId: " + notificationId); 84 85 setupAlert(); 86 } 87 88 @Override 89 protected void onResume() { 90 super.onResume(); 91 if (DEBUG) Log.d(TAG, "onResume"); 92 registerReceiver(mNetInitiatedReceiver, new IntentFilter(GpsNetInitiatedHandler.ACTION_NI_VERIFY)); 93 } 94 95 @Override 96 protected void onPause() { 97 super.onPause(); 98 if (DEBUG) Log.d(TAG, "onPause"); 99 unregisterReceiver(mNetInitiatedReceiver); 100 } 101 102 /** 103 * {@inheritDoc} 104 */ 105 public void onClick(DialogInterface dialog, int which) { 106 if (which == POSITIVE_BUTTON) { 107 sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT); 108 } 109 if (which == NEGATIVE_BUTTON) { 110 sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_DENY); 111 } 112 113 // No matter what, finish the activity 114 finish(); 115 notificationId = -1; 116 } 117 118 // Respond to NI Handler under GpsLocationProvider, 1 = accept, 2 = deny 119 private void sendUserResponse(int response) { 120 if (DEBUG) Log.d(TAG, "sendUserResponse, response: " + response); 121 LocationManager locationManager = (LocationManager) 122 this.getSystemService(Context.LOCATION_SERVICE); 123 locationManager.sendNiResponse(notificationId, response); 124 } 125 126 private void handleNIVerify(Intent intent) { 127 int notifId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1); 128 notificationId = notifId; 129 130 if (DEBUG) Log.d(TAG, "handleNIVerify action: " + intent.getAction()); 131 } 132 133 private void showNIError() { 134 Toast.makeText(this, "NI error" /* com.android.internal.R.string.usb_storage_error_message */, 135 Toast.LENGTH_LONG).show(); 136 } 137} 138