Home | History | Annotate | Download | only in app
      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 
     17 package com.android.internal.app;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.content.Intent;
     24 import android.content.IntentFilter;
     25 import android.os.Bundle;
     26 import android.os.Handler;
     27 import android.os.Message;
     28 import android.os.RemoteException;
     29 import android.os.ServiceManager;
     30 import android.widget.Toast;
     31 import android.util.Log;
     32 import android.location.LocationManager;
     33 import com.android.internal.location.GpsLocationProvider;
     34 import com.android.internal.location.GpsNetInitiatedHandler;
     35 
     36 /**
     37  * This activity is shown to the user for him/her to accept or deny network-initiated
     38  * requests. It uses the alert dialog style. It will be launched from a notification.
     39  */
     40 public class NetInitiatedActivity extends AlertActivity implements DialogInterface.OnClickListener {
     41 
     42     private static final String TAG = "NetInitiatedActivity";
     43 
     44     private static final boolean DEBUG = true;
     45     private static final boolean VERBOSE = false;
     46 
     47     private static final int POSITIVE_BUTTON = AlertDialog.BUTTON1;
     48     private static final int NEGATIVE_BUTTON = AlertDialog.BUTTON2;
     49 
     50     // Dialog button text
     51     public static final String BUTTON_TEXT_ACCEPT = "Accept";
     52     public static final String BUTTON_TEXT_DENY = "Deny";
     53 
     54     // Received ID from intent, -1 when no notification is in progress
     55     private int notificationId = -1;
     56 
     57     /** Used to detect when NI request is received */
     58     private BroadcastReceiver mNetInitiatedReceiver = new BroadcastReceiver() {
     59         @Override
     60         public void onReceive(Context context, Intent intent) {
     61             if (DEBUG) Log.d(TAG, "NetInitiatedReceiver onReceive: " + intent.getAction());
     62             if (intent.getAction() == GpsNetInitiatedHandler.ACTION_NI_VERIFY) {
     63                 handleNIVerify(intent);
     64             }
     65         }
     66     };
     67 
     68     @Override
     69     protected void onCreate(Bundle savedInstanceState) {
     70         super.onCreate(savedInstanceState);
     71 
     72         // Set up the "dialog"
     73         final Intent intent = getIntent();
     74         final AlertController.AlertParams p = mAlertParams;
     75         p.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
     76         p.mTitle = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TITLE);
     77         p.mMessage = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_MESSAGE);
     78         p.mPositiveButtonText = BUTTON_TEXT_ACCEPT;
     79         p.mPositiveButtonListener = this;
     80         p.mNegativeButtonText = BUTTON_TEXT_DENY;
     81         p.mNegativeButtonListener = this;
     82 
     83         notificationId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
     84         if (DEBUG) Log.d(TAG, "onCreate, notifId: " + notificationId);
     85 
     86         setupAlert();
     87     }
     88 
     89     @Override
     90     protected void onResume() {
     91         super.onResume();
     92         if (DEBUG) Log.d(TAG, "onResume");
     93         registerReceiver(mNetInitiatedReceiver, new IntentFilter(GpsNetInitiatedHandler.ACTION_NI_VERIFY));
     94     }
     95 
     96     @Override
     97     protected void onPause() {
     98         super.onPause();
     99         if (DEBUG) Log.d(TAG, "onPause");
    100         unregisterReceiver(mNetInitiatedReceiver);
    101     }
    102 
    103     /**
    104      * {@inheritDoc}
    105      */
    106     public void onClick(DialogInterface dialog, int which) {
    107         if (which == POSITIVE_BUTTON) {
    108             sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT);
    109         }
    110         if (which == NEGATIVE_BUTTON) {
    111             sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_DENY);
    112         }
    113 
    114         // No matter what, finish the activity
    115         finish();
    116         notificationId = -1;
    117     }
    118 
    119     // Respond to NI Handler under GpsLocationProvider, 1 = accept, 2 = deny
    120     private void sendUserResponse(int response) {
    121         if (DEBUG) Log.d(TAG, "sendUserResponse, response: " + response);
    122         LocationManager locationManager = (LocationManager)
    123             this.getSystemService(Context.LOCATION_SERVICE);
    124         locationManager.sendNiResponse(notificationId, response);
    125     }
    126 
    127     private void handleNIVerify(Intent intent) {
    128         int notifId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
    129         notificationId = notifId;
    130 
    131         if (DEBUG) Log.d(TAG, "handleNIVerify action: " + intent.getAction());
    132     }
    133 
    134     private void showNIError() {
    135         Toast.makeText(this, "NI error" /* com.android.internal.R.string.usb_storage_error_message */,
    136                 Toast.LENGTH_LONG).show();
    137     }
    138 }
    139