import java.lang.ref.WeakReference;
import java.util.*;
+import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
-import android.support.v4.app.FragmentActivity;
-import android.support.v4.app.FragmentManager;
+import androidx.fragment.app.FragmentActivity;
+import androidx.fragment.app.FragmentManager;
import android.view.*;
import android.widget.*;
import android.app.AlertDialog;
import android.location.LocationManager;
import android.location.LocationListener;
import android.hardware.usb.*;
-
+import android.content.pm.PackageManager;
+import androidx.core.app.ActivityCompat;
import org.altusmetrum.altoslib_13.*;
class SavedState {
}
}
-public class AltosDroid extends FragmentActivity implements AltosUnitsListener, LocationListener {
+public class AltosDroid extends FragmentActivity implements AltosUnitsListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
// Actions sent to the telemetry server at startup time
mTab.update_ui(telem_state, state, from_receiver, location, mTab == mTabsAdapter.currentItem());
AltosDebug.debug("quiet %b\n", quiet);
- if (mAltosVoice != null)
+ if (mAltosVoice != null && mTabsAdapter.currentItem() != null)
mAltosVoice.tell(telem_state, state, from_receiver, location, (AltosDroidTab) mTabsAdapter.currentItem(), quiet);
}
noticeIntent(intent);
}
- @Override
- public void onResume() {
- super.onResume();
- AltosDebug.debug("+ ON RESUME +");
-
+ private void enable_location_updates() {
// Listen for GPS and Network position updates
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
update_ui(telemetry_state, state, true);
}
+ static final int MY_PERMISSION_REQUEST_FINE_POSITION = 1001;
+
+ public boolean have_location_permission = false;
+ public boolean asked_location_permission = false;
+
+ AltosMapOnline map_online;
+
+ void
+ tell_map_permission(AltosMapOnline map_online) {
+ this.map_online = map_online;
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode, String[] permissions,
+ int[] grantResults) {
+ switch (requestCode) {
+ case MY_PERMISSION_REQUEST_FINE_POSITION:
+ if (grantResults.length > 0 &&
+ grantResults[0] == PackageManager.PERMISSION_GRANTED) {
+ have_location_permission = true;
+ enable_location_updates();
+ if (map_online != null)
+ map_online.position_permission();
+ }
+ break;
+ }
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ AltosDebug.debug("+ ON RESUME +");
+
+ if (!asked_location_permission) {
+ asked_location_permission = true;
+ if (ActivityCompat.checkSelfPermission(this,
+ Manifest.permission.ACCESS_FINE_LOCATION)
+ == PackageManager.PERMISSION_GRANTED)
+ {
+ have_location_permission = true;
+ }
+ else
+ {
+ ActivityCompat.requestPermissions(this,
+ new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
+ MY_PERMISSION_REQUEST_FINE_POSITION);
+ }
+ }
+ if (have_location_permission)
+ enable_location_updates();
+ }
+
@Override
public void onPause() {
super.onPause();
AltosDebug.debug("- ON PAUSE -");
// Stop listening for location updates
- ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
+ if (have_location_permission)
+ ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
}
@Override
}
private void bluetoothEnabled(Intent data) {
- try {
- mService.send(Message.obtain(null, TelemetryService.MSG_BLUETOOTH_ENABLED, null));
- } catch (RemoteException e) {
- AltosDebug.debug("send BT enabled message failed");
+ if (mService != null) {
+ try {
+ mService.send(Message.obtain(null, TelemetryService.MSG_BLUETOOTH_ENABLED, null));
+ } catch (RemoteException e) {
+ AltosDebug.debug("send BT enabled message failed");
+ }
}
}