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