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.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
public static final int SETUP_UNITS = 2;
public static final int SETUP_MAP_SOURCE = 4;
public static final int SETUP_MAP_TYPE = 8;
+ public static final int SETUP_FONT_SIZE = 16;
public static FragmentManager fm;
for (AltosDroidTab mTab : mTabs)
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);
}
}
int deg = (int) Math.floor(p);
double min = (p - Math.floor(p)) * 60.0;
- return String.format("%d°%9.4f\" %s", deg, min, h);
+ return String.format("%d° %7.4f\" %s", deg, min, h);
}
static String number(String format, double value) {
return tab_view;
}
+ static public int[] themes = {
+ R.style.Small,
+ R.style.Medium,
+ R.style.Large,
+ R.style.Extra
+ };
+
+ static public int[] dialog_themes = {
+ R.style.Small_Dialog,
+ R.style.Medium_Dialog,
+ R.style.Large_Dialog,
+ R.style.Extra_Dialog
+ };
+
@Override
public void onCreate(Bundle savedInstanceState) {
+ // Initialise preferences
+ AltosDroidPreferences.init(this);
+ setTheme(themes[AltosDroidPreferences.font_size()]);
super.onCreate(savedInstanceState);
AltosDebug.init(this);
AltosDebug.debug("+++ ON CREATE +++");
- // Initialise preferences
- AltosDroidPreferences.init(this);
fm = getSupportFragmentManager();
mStateView = (TextView) findViewById(R.id.state_value);
mAgeView = (TextView) findViewById(R.id.age_value);
mAgeNewColor = mAgeView.getTextColors().getDefaultColor();
- mAgeOldColor = getResources().getColor(R.color.old_color);
+ mAgeOldColor = getResources().getColor(R.color.old_color, getTheme());
}
private void ensureBluetooth() {
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 = 1001;
+
+ public boolean have_location_permission = false;
+ public boolean have_storage_permission = false;
+ public boolean asked_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) {
+ if (requestCode == MY_PERMISSION_REQUEST) {
+ for (int i = 0; i < grantResults.length; i++) {
+ if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
+ if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
+ have_location_permission = true;
+ enable_location_updates();
+ if (map_online != null)
+ map_online.position_permission();
+ }
+ if (permissions[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
+ have_storage_permission = true;
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ AltosDebug.debug("+ ON RESUME +");
+
+ if (!asked_permission) {
+ asked_permission = true;
+ if (ActivityCompat.checkSelfPermission(this,
+ Manifest.permission.ACCESS_FINE_LOCATION)
+ == PackageManager.PERMISSION_GRANTED)
+ {
+ have_location_permission = true;
+ }
+ if (ActivityCompat.checkSelfPermission(this,
+ Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ == PackageManager.PERMISSION_GRANTED)
+ {
+ have_storage_permission = true;
+ }
+ int count = (have_location_permission ? 0 : 1) + (have_storage_permission ? 0 : 1);
+ if (count > 0)
+ {
+ String[] permissions = new String[count];
+ int i = 0;
+ if (!have_location_permission)
+ permissions[i++] = Manifest.permission.ACCESS_FINE_LOCATION;
+ if (!have_location_permission)
+ permissions[i++] = Manifest.permission.WRITE_EXTERNAL_STORAGE;
+ ActivityCompat.requestPermissions(this, permissions, MY_PERMISSION_REQUEST);
+ }
+ }
+ 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
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- AltosDebug.debug("onActivityResult " + resultCode);
+ AltosDebug.debug("onActivityResult request %d result %d", requestCode, resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect to
private void note_setup_changes(Intent data) {
int changes = data.getIntExtra(SetupActivity.EXTRA_SETUP_CHANGES, 0);
+ AltosDebug.debug("note_setup_changes changes %d\n", changes);
+
if ((changes & SETUP_BAUD) != 0) {
try {
mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD,
/* nothing to do here */
}
set_switch_time();
+ if ((changes & SETUP_FONT_SIZE) != 0) {
+ AltosDebug.debug(" ==== Recreate to switch font sizes ==== ");
+ finish();
+ startActivity(getIntent());
+ }
}
private void connectUsb(UsbDevice device) {
}
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");
+ }
}
}