1 package com.android.cts.verifier.location;
2 
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.location.Location;
6 import android.location.LocationListener;
7 import android.location.LocationManager;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.os.Message;
11 import android.os.Process;
12 import android.provider.Settings;
13 import android.util.Log;
14 import android.widget.Toast;
15 
16 import com.android.cts.verifier.R;
17 
18 public class LocationListenerActivity extends Activity implements Handler.Callback {
19     // Primary -> managed intent: request to goto the location settings page and listen to updates.
20     public static final String ACTION_SET_LOCATION_AND_CHECK_UPDATES =
21             "com.android.cts.verifier.location.SET_LOCATION_AND_CHECK";
22     private static final int REQUEST_LOCATION_UPDATE = 1;
23 
24     private static final int MSG_TIMEOUT_ID = 1;
25 
26     private static final long MSG_TIMEOUT_MILLISEC = 15000; // 15 seconds.
27 
28     private LocationManager mLocationManager;
29     private Handler mHandler;
30     private boolean mIsLocationUpdated;
31 
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35         mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
36         mHandler = new Handler(this);
37         mIsLocationUpdated = false;
38         Intent intent = getIntent();
39         if (intent != null) {
40             String action = intent.getAction();
41             if (ACTION_SET_LOCATION_AND_CHECK_UPDATES.equals(action)) {
42                 Log.d(getLogTag(), "ACTION_SET_LOCATION_AND_CHECK_UPDATES received in uid "
43                         + Process.myUid());
44                 handleLocationAction();
45             }
46         }
47     }
48 
49     @Override
onActivityResult(int requestCode, int resultCode, Intent data)50     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
51         switch (requestCode) {
52             case REQUEST_LOCATION_UPDATE: {
53                 Log.d(getLogTag(), "Exit location settings:OK");
54                 mLocationManager.removeUpdates(mLocationListener);
55                 mHandler.removeMessages(MSG_TIMEOUT_ID);
56                 finish();
57                 break;
58             }
59             default: {
60                 Log.wtf(getLogTag(), "Unknown requestCode " + requestCode + "; data = " + data);
61                 break;
62             }
63         }
64     }
65 
handleLocationAction()66     protected void handleLocationAction() {
67         Intent locationSettingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
68         if (locationSettingsIntent.resolveActivity(getPackageManager()) != null) {
69             startActivityForResult(locationSettingsIntent, REQUEST_LOCATION_UPDATE);
70             scheduleTimeout();
71         } else {
72             Log.e(getLogTag(), "Settings.ACTION_LOCATION_SOURCE_SETTINGS could not be resolved");
73             finish();
74         }
75         mLocationManager.requestLocationUpdates(
76                 LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
77     }
78 
79     private final LocationListener mLocationListener = new LocationListener() {
80         @Override
81         public void onLocationChanged(Location location) {
82             synchronized (LocationListenerActivity.this) {
83                 if (mIsLocationUpdated) return;
84                 showToast(R.string.provisioning_byod_location_mode_enable_toast_location_change);
85                 mIsLocationUpdated = true;
86             }
87         }
88 
89         @Override
90         public void onProviderDisabled(String provider) {
91         }
92 
93         @Override
94         public void onProviderEnabled(String provider) {
95         }
96 
97         @Override
98         public void onStatusChanged(String provider, int status, Bundle extras) {
99         }
100     };
101 
scheduleTimeout()102     private void scheduleTimeout() {
103         mHandler.removeMessages(MSG_TIMEOUT_ID);
104         mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_TIMEOUT_ID), MSG_TIMEOUT_MILLISEC);
105     }
106 
107     @Override
handleMessage(Message msg)108     public boolean handleMessage(Message msg) {
109         if (msg.what == MSG_TIMEOUT_ID) {
110             synchronized (this) {
111                 if (mIsLocationUpdated) return true;
112                 showToast(R.string.provisioning_byod_location_mode_time_out_toast);
113             }
114         }
115         return true;
116     }
117 
getLogTag()118     protected String getLogTag() {
119         return "LocationListenerActivity";
120     }
121 
showToast(int messageId)122     protected void showToast(int messageId) {
123         String message = getString(messageId);
124         Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
125     }
126 }
127