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