Merge pull request #1936 from MilosKozak/batteryopt

Battery permission fix
This commit is contained in:
Milos Kozak 2019-08-03 23:50:30 +02:00 committed by GitHub
commit 3808e4b48b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 515 additions and 643 deletions

View file

@ -335,7 +335,7 @@ public class MainActivity extends AppCompatActivity {
case AndroidPermission.CASE_LOCATION: case AndroidPermission.CASE_LOCATION:
case AndroidPermission.CASE_SMS: case AndroidPermission.CASE_SMS:
case AndroidPermission.CASE_BATTERY: case AndroidPermission.CASE_BATTERY:
case AndroidPermission.CASE_PHONESTATE: case AndroidPermission.CASE_PHONE_STATE:
break; break;
} }
} }

View file

@ -11,11 +11,7 @@ class RequestDexcomPermissionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(SourceDexcomPlugin.PERMISSION), requestCode) requestPermissions(arrayOf(SourceDexcomPlugin.PERMISSION), requestCode)
} else {
finish()
}
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {

View file

@ -1,15 +1,5 @@
package info.nightscout.androidaps.plugins.pump.common.dialog; package info.nightscout.androidaps.plugins.pump.common.dialog;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.Manifest; import android.Manifest;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@ -21,7 +11,6 @@ import android.bluetooth.le.ScanSettings;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.ParcelUuid; import android.os.ParcelUuid;
@ -41,6 +30,16 @@ import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst;
@ -89,7 +88,7 @@ public class RileyLinkBLEScanActivity extends AppCompatActivity {
mHandler = new Handler(); mHandler = new Handler();
mLeDeviceListAdapter = new LeDeviceListAdapter(); mLeDeviceListAdapter = new LeDeviceListAdapter();
listBTScan = (ListView)findViewById(R.id.rileylink_listBTScan); listBTScan = (ListView) findViewById(R.id.rileylink_listBTScan);
listBTScan.setAdapter(mLeDeviceListAdapter); listBTScan.setAdapter(mLeDeviceListAdapter);
listBTScan.setOnItemClickListener((parent, view, position, id) -> { listBTScan.setOnItemClickListener((parent, view, position, id) -> {
@ -99,7 +98,7 @@ public class RileyLinkBLEScanActivity extends AppCompatActivity {
mLEScanner.stopScan(mScanCallback2); mLEScanner.stopScan(mScanCallback2);
} }
TextView textview = (TextView)view.findViewById(R.id.rileylink_device_address); TextView textview = (TextView) view.findViewById(R.id.rileylink_device_address);
String bleAddress = textview.getText().toString(); String bleAddress = textview.getText().toString();
SP.putString(RileyLinkConst.Prefs.RileyLinkAddress, bleAddress); SP.putString(RileyLinkConst.Prefs.RileyLinkAddress, bleAddress);
@ -114,7 +113,7 @@ public class RileyLinkBLEScanActivity extends AppCompatActivity {
finish(); finish();
}); });
toolbarBTScan = (Toolbar)findViewById(R.id.rileylink_toolbarBTScan); toolbarBTScan = (Toolbar) findViewById(R.id.rileylink_toolbarBTScan);
toolbarBTScan.setTitle(R.string.rileylink_scanner_title); toolbarBTScan.setTitle(R.string.rileylink_scanner_title);
setSupportActionBar(toolbarBTScan); setSupportActionBar(toolbarBTScan);
@ -161,7 +160,7 @@ public class RileyLinkBLEScanActivity extends AppCompatActivity {
// you can selectively disable BLE-related features. // you can selectively disable BLE-related features.
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// your code that requires permission // your code that requires permission
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION); PERMISSION_REQUEST_COARSE_LOCATION);
} }
@ -171,15 +170,10 @@ public class RileyLinkBLEScanActivity extends AppCompatActivity {
Toast.makeText(this, R.string.rileylink_scanner_ble_not_enabled, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.rileylink_scanner_ble_not_enabled, Toast.LENGTH_SHORT).show();
} else { } else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Will request that GPS be enabled for devices running Marshmallow or newer. // Will request that GPS be enabled for devices running Marshmallow or newer.
if (!LocationHelper.isLocationEnabled(this)) { if (!LocationHelper.isLocationEnabled(this)) {
LocationHelper.requestLocationForBluetooth(this); LocationHelper.requestLocationForBluetooth(this);
} }
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
mLEScanner = mBluetoothAdapter.getBluetoothLeScanner(); mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(); settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
@ -384,11 +378,11 @@ public class RileyLinkBLEScanActivity extends AppCompatActivity {
if (view == null) { if (view == null) {
view = mInflator.inflate(R.layout.rileylink_scan_item, null); view = mInflator.inflate(R.layout.rileylink_scan_item, null);
viewHolder = new ViewHolder(); viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView)view.findViewById(R.id.rileylink_device_address); viewHolder.deviceAddress = (TextView) view.findViewById(R.id.rileylink_device_address);
viewHolder.deviceName = (TextView)view.findViewById(R.id.rileylink_device_name); viewHolder.deviceName = (TextView) view.findViewById(R.id.rileylink_device_name);
view.setTag(viewHolder); view.setTag(viewHolder);
} else { } else {
viewHolder = (ViewHolder)view.getTag(); viewHolder = (ViewHolder) view.getTag();
} }
BluetoothDevice device = mLeDevices.get(i); BluetoothDevice device = mLeDevices.get(i);

View file

@ -24,7 +24,7 @@ public class LocationHelper {
* @return true if location is enabled, false otherwise. * @return true if location is enabled, false otherwise.
*/ */
public static boolean isLocationEnabled(Context context) { public static boolean isLocationEnabled(Context context) {
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return (locationManager != null && // return (locationManager != null && //
(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || // (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || //
@ -69,10 +69,8 @@ public class LocationHelper {
*/ */
public static void requestLocationForBluetooth(Activity activity) { public static void requestLocationForBluetooth(Activity activity) {
// Location needs to be enabled for Bluetooth discovery on Marshmallow. // Location needs to be enabled for Bluetooth discovery on Marshmallow.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
LocationHelper.requestLocation(activity); LocationHelper.requestLocation(activity);
} }
}
// public static Boolean locationPermission(ActivityWithMenu act) { // public static Boolean locationPermission(ActivityWithMenu act) {
// return ActivityCompat.checkSelfPermission(act, Manifest.permission.ACCESS_FINE_LOCATION) == // return ActivityCompat.checkSelfPermission(act, Manifest.permission.ACCESS_FINE_LOCATION) ==

View file

@ -44,7 +44,7 @@ object SourceDexcomPlugin : PluginBase(PluginDescription()
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(MainApp.instance(), PERMISSION) != PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(MainApp.instance(), PERMISSION) != PackageManager.PERMISSION_GRANTED) {
val intent = Intent(MainApp.instance(), RequestDexcomPermissionActivity::class.java) val intent = Intent(MainApp.instance(), RequestDexcomPermissionActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
MainApp.instance().startActivity(intent) MainApp.instance().startActivity(intent)

View file

@ -2,14 +2,11 @@ package info.nightscout.androidaps.setupwizard;
import android.Manifest; import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -21,18 +18,19 @@ import info.nightscout.androidaps.events.EventConfigBuilderChange;
import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesFragment; import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesFragment;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment;
import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow;
import info.nightscout.androidaps.plugins.general.maintenance.ImportExportPrefs; import info.nightscout.androidaps.plugins.general.maintenance.ImportExportPrefs;
import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus;
import info.nightscout.androidaps.plugins.general.nsclient.services.NSClientService;
import info.nightscout.androidaps.plugins.profile.local.LocalProfileFragment; import info.nightscout.androidaps.plugins.profile.local.LocalProfileFragment;
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin; import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin;
import info.nightscout.androidaps.plugins.profile.ns.NSProfileFragment; import info.nightscout.androidaps.plugins.profile.ns.NSProfileFragment;
@ -56,8 +54,6 @@ import info.nightscout.androidaps.utils.PasswordProtection;
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
public class SWDefinition { public class SWDefinition {
private static Logger log = LoggerFactory.getLogger(SWDefinition.class);
private AppCompatActivity activity; private AppCompatActivity activity;
private List<SWScreen> screens = new ArrayList<>(); private List<SWScreen> screens = new ArrayList<>();
@ -69,29 +65,29 @@ public class SWDefinition {
return activity; return activity;
} }
public List<SWScreen> getScreens() { List<SWScreen> getScreens() {
return screens; return screens;
} }
SWDefinition add(SWScreen newScreen) { private SWDefinition add(SWScreen newScreen) {
screens.add(newScreen); screens.add(newScreen);
return this; return this;
} }
SWDefinition() { SWDefinition() {
if (Config.APS || Config.PUMPCONTROL) if (Config.APS)
SWDefinitionFull(); SWDefinitionFull();
else if (Config.PUMPCONTROL)
SWDefinitionPumpControl();
else if (Config.NSCLIENT) else if (Config.NSCLIENT)
SWDefinitionNSClient(); SWDefinitionNSClient();
} }
private void SWDefinitionFull() { private SWScreen screenSetupWizard = new SWScreen(R.string.nav_setupwizard)
// List all the screens here
add(new SWScreen(R.string.nav_setupwizard)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.welcometosetupwizard)) .label(R.string.welcometosetupwizard));
)
.add(new SWScreen(R.string.language) private SWScreen screenLanguage = new SWScreen(R.string.language)
.skippable(false) .skippable(false)
.add(new SWRadioButton() .add(new SWRadioButton()
.option(R.array.languagesArray, R.array.languagesValues) .option(R.array.languagesArray, R.array.languagesValues)
@ -101,9 +97,9 @@ public class SWDefinition {
String lang = SP.getString("language", "en"); String lang = SP.getString("language", "en");
LocaleHelper.setLocale(MainApp.instance().getApplicationContext(), lang); LocaleHelper.setLocale(MainApp.instance().getApplicationContext(), lang);
return SP.contains(R.string.key_language); return SP.contains(R.string.key_language);
}) });
)
.add(new SWScreen(R.string.end_user_license_agreement) private SWScreen screenEula = new SWScreen(R.string.end_user_license_agreement)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.end_user_license_agreement_text)) .label(R.string.end_user_license_agreement_text))
@ -116,54 +112,54 @@ public class SWDefinition {
MainApp.bus().post(new EventSWUpdate(false)); MainApp.bus().post(new EventSWUpdate(false));
})) }))
.visibility(() -> !SP.getBoolean(R.string.key_i_understand, false)) .visibility(() -> !SP.getBoolean(R.string.key_i_understand, false))
.validator(() -> SP.getBoolean(R.string.key_i_understand, false)) .validator(() -> SP.getBoolean(R.string.key_i_understand, false));
)
.add(new SWScreen(R.string.permission) private SWScreen screenPermissionBattery = new SWScreen(R.string.permission)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name)))) .label(String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name))))
.add(new SWBreak()) .add(new SWBreak())
.add(new SWButton() .add(new SWButton()
.text(R.string.askforpermission) .text(R.string.askforpermission)
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) .visibility(() -> AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS))
.action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, AndroidPermission.CASE_BATTERY))) .action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, AndroidPermission.CASE_BATTERY)))
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) .visibility(() -> AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS))
.validator(() -> !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS))) .validator(() -> !(AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)));
)
.add(new SWScreen(R.string.permission) private SWScreen screenPermissionBt = new SWScreen(R.string.permission)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(MainApp.gs(R.string.needlocationpermission))) .label(MainApp.gs(R.string.needlocationpermission)))
.add(new SWBreak()) .add(new SWBreak())
.add(new SWButton() .add(new SWButton()
.text(R.string.askforpermission) .text(R.string.askforpermission)
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) .visibility(() -> AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION))
.action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION, AndroidPermission.CASE_LOCATION))) .action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION, AndroidPermission.CASE_LOCATION)))
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) .visibility(() -> AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION))
.validator(() -> !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION))) .validator(() -> !(AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)));
)
.add(new SWScreen(R.string.permission) private SWScreen screenPermissionStore = new SWScreen(R.string.permission)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(MainApp.gs(R.string.needstoragepermission))) .label(MainApp.gs(R.string.needstoragepermission)))
.add(new SWBreak()) .add(new SWBreak())
.add(new SWButton() .add(new SWButton()
.text(R.string.askforpermission) .text(R.string.askforpermission)
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) .visibility(() -> AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE))
.action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, AndroidPermission.CASE_STORAGE))) .action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, AndroidPermission.CASE_STORAGE)))
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) .visibility(() -> AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE))
.validator(() -> !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE))) .validator(() -> !(AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)));
)
.add(new SWScreen(R.string.nav_import) private SWScreen screenImport = new SWScreen(R.string.nav_import)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.storedsettingsfound)) .label(R.string.storedsettingsfound))
.add(new SWBreak()) .add(new SWBreak())
.add(new SWButton() .add(new SWButton()
.text(R.string.nav_import) .text(R.string.nav_import)
.action(() -> ImportExportPrefs.importSharedPreferences(getActivity()))) .action(() -> ImportExportPrefs.importSharedPreferences(getActivity())))
.visibility(() -> ImportExportPrefs.file.exists() && !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE))) .visibility(() -> ImportExportPrefs.file.exists() && !(AndroidPermission.permissionNotGranted(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)));
)
.add(new SWScreen(R.string.nsclientinternal_title) private SWScreen screenNsClient = new SWScreen(R.string.nsclientinternal_title)
.skippable(true) .skippable(true)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.nsclientinfotext)) .label(R.string.nsclientinfotext))
@ -202,10 +198,10 @@ public class SWDefinition {
}) })
) )
.add(new SWBreak()) .add(new SWBreak())
.validator(() -> NSClientPlugin.getPlugin().nsClientService != null && NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth) .validator(() -> NSClientPlugin.getPlugin().nsClientService != null && NSClientService.isConnected && NSClientService.hasWriteAuth)
.visibility(() -> !(NSClientPlugin.getPlugin().nsClientService != null && NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth)) .visibility(() -> !(NSClientPlugin.getPlugin().nsClientService != null && NSClientService.isConnected && NSClientService.hasWriteAuth));
)
.add(new SWScreen(R.string.patientage) private SWScreen screenAge = new SWScreen(R.string.patientage)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.patientage_summary)) .label(R.string.patientage_summary))
@ -215,9 +211,9 @@ public class SWDefinition {
.preferenceId(R.string.key_age) .preferenceId(R.string.key_age)
.label(R.string.patientage) .label(R.string.patientage)
.comment(R.string.patientage_summary)) .comment(R.string.patientage_summary))
.validator(() -> SP.contains(R.string.key_age)) .validator(() -> SP.contains(R.string.key_age));
)
.add(new SWScreen(R.string.configbuilder_insulin) private SWScreen screenInsulin = new SWScreen(R.string.configbuilder_insulin)
.skippable(false) .skippable(false)
.add(new SWPlugin() .add(new SWPlugin()
.option(PluginType.INSULIN, R.string.configbuilder_insulin_description) .option(PluginType.INSULIN, R.string.configbuilder_insulin_description)
@ -231,16 +227,18 @@ public class SWDefinition {
.text(R.string.insulinsourcesetup) .text(R.string.insulinsourcesetup)
.action(() -> { .action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveInsulin(); final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveInsulin();
if (plugin != null) {
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> { PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class); Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId()); i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i); activity.startActivity(i);
}, null); }, null);
}
}) })
.visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveInsulin()!= null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveInsulin()).getPreferencesId() > 0)) .visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveInsulin() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveInsulin()).getPreferencesId() > 0))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveInsulin() != null) .validator(() -> ConfigBuilderPlugin.getPlugin().getActiveInsulin() != null);
)
.add(new SWScreen(R.string.configbuilder_bgsource) private SWScreen screenBgSource = new SWScreen(R.string.configbuilder_bgsource)
.skippable(false) .skippable(false)
.add(new SWPlugin() .add(new SWPlugin()
.option(PluginType.BGSOURCE, R.string.configbuilder_bgsource_description) .option(PluginType.BGSOURCE, R.string.configbuilder_bgsource_description)
@ -250,16 +248,18 @@ public class SWDefinition {
.text(R.string.bgsourcesetup) .text(R.string.bgsourcesetup)
.action(() -> { .action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveBgSource(); final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveBgSource();
if (plugin != null) {
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> { PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class); Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId()); i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i); activity.startActivity(i);
}, null); }, null);
}
}) })
.visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveBgSource()!= null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveBgSource()).getPreferencesId() > 0)) .visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveBgSource() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveBgSource()).getPreferencesId() > 0))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveBgSource() != null) .validator(() -> ConfigBuilderPlugin.getPlugin().getActiveBgSource() != null);
)
.add(new SWScreen(R.string.configbuilder_profile) private SWScreen screenProfile = new SWScreen(R.string.configbuilder_profile)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.setupwizard_profile_description)) .label(R.string.setupwizard_profile_description))
@ -267,32 +267,32 @@ public class SWDefinition {
.add(new SWPlugin() .add(new SWPlugin()
.option(PluginType.PROFILE, R.string.configbuilder_profile_description) .option(PluginType.PROFILE, R.string.configbuilder_profile_description)
.label(R.string.configbuilder_profile)) .label(R.string.configbuilder_profile))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null) .validator(() -> ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null);
)
.add(new SWScreen(R.string.nsprofile) private SWScreen screenNsProfile = new SWScreen(R.string.nsprofile)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.adjustprofileinns)) .label(R.string.adjustprofileinns))
.add(new SWFragment(this) .add(new SWFragment(this)
.add(new NSProfileFragment())) .add(new NSProfileFragment()))
.validator(() -> NSProfilePlugin.getPlugin().getProfile() != null && NSProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard")) .validator(() -> NSProfilePlugin.getPlugin().getProfile() != null && NSProfilePlugin.getPlugin().getProfile().getDefaultProfile() != null && NSProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard"))
.visibility(() -> NSProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE)) .visibility(() -> NSProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE));
)
.add(new SWScreen(R.string.localprofile) private SWScreen screenLocalProfile = new SWScreen(R.string.localprofile)
.skippable(false) .skippable(false)
.add(new SWFragment(this) .add(new SWFragment(this)
.add(new LocalProfileFragment())) .add(new LocalProfileFragment()))
.validator(() -> LocalProfilePlugin.getPlugin().getProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard")) .validator(() -> LocalProfilePlugin.getPlugin().getProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard"))
.visibility(() -> LocalProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE)) .visibility(() -> LocalProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE));
)
.add(new SWScreen(R.string.simpleprofile) private SWScreen screenSimpleProfile = new SWScreen(R.string.simpleprofile)
.skippable(false) .skippable(false)
.add(new SWFragment(this) .add(new SWFragment(this)
.add(new SimpleProfileFragment())) .add(new SimpleProfileFragment()))
.validator(() -> SimpleProfilePlugin.getPlugin().getProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard")) .validator(() -> SimpleProfilePlugin.getPlugin().getProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard"))
.visibility(() -> SimpleProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE)) .visibility(() -> SimpleProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE));
)
.add(new SWScreen(R.string.profileswitch) private SWScreen screenProfileSwitch = new SWScreen(R.string.profileswitch)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.profileswitch_ismissing)) .label(R.string.profileswitch_ismissing))
@ -300,15 +300,15 @@ public class SWDefinition {
.text(R.string.profileswitch) .text(R.string.profileswitch)
.action(() -> { .action(() -> {
NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog();
final OptionsToShow profileswitch = CareportalFragment.PROFILESWITCHDIRECT; final OptionsToShow profileSwitch = CareportalFragment.PROFILESWITCHDIRECT;
profileswitch.executeProfileSwitch = true; profileSwitch.executeProfileSwitch = true;
newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); newDialog.setOptions(profileSwitch, R.string.careportal_profileswitch);
newDialog.show(getActivity().getSupportFragmentManager(), "NewNSTreatmentDialog"); newDialog.show(getActivity().getSupportFragmentManager(), "NewNSTreatmentDialog");
})) }))
.validator(() -> ProfileFunctions.getInstance().getProfile() != null) .validator(() -> ProfileFunctions.getInstance().getProfile() != null)
.visibility(() -> ProfileFunctions.getInstance().getProfile() == null) .visibility(() -> ProfileFunctions.getInstance().getProfile() == null);
)
.add(new SWScreen(R.string.configbuilder_pump) private SWScreen screenPump = new SWScreen(R.string.configbuilder_pump)
.skippable(false) .skippable(false)
.add(new SWPlugin() .add(new SWPlugin()
.option(PluginType.PUMP, R.string.configbuilder_pump_description) .option(PluginType.PUMP, R.string.configbuilder_pump_description)
@ -318,13 +318,15 @@ public class SWDefinition {
.text(R.string.pumpsetup) .text(R.string.pumpsetup)
.action(() -> { .action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActivePump(); final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActivePump();
if (plugin != null) {
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> { PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class); Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId()); i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i); activity.startActivity(i);
}, null); }, null);
}
}) })
.visibility(() -> ((PluginBase) ConfigBuilderPlugin.getPlugin().getActivePump()).getPreferencesId() > 0)) .visibility(() -> (ConfigBuilderPlugin.getPlugin().getActivePump() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActivePump()).getPreferencesId() > 0)))
.add(new SWButton() .add(new SWButton()
.text(R.string.readstatus) .text(R.string.readstatus)
.action(() -> ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("Clicked connect to pump", null)) .action(() -> ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("Clicked connect to pump", null))
@ -337,9 +339,9 @@ public class SWDefinition {
} }
}) })
) )
.validator(() -> ConfigBuilderPlugin.getPlugin().getActivePump() != null && ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized()) .validator(() -> ConfigBuilderPlugin.getPlugin().getActivePump() != null && ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized());
)
.add(new SWScreen(R.string.configbuilder_aps) private SWScreen screenAps = new SWScreen(R.string.configbuilder_aps)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.setupwizard_aps_description)) .label(R.string.setupwizard_aps_description))
@ -354,25 +356,27 @@ public class SWDefinition {
.text(R.string.apssetup) .text(R.string.apssetup)
.action(() -> { .action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveAPS(); final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveAPS();
if (plugin != null) {
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> { PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class); Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId()); i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i); activity.startActivity(i);
}, null); }, null);
}
}) })
.visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveAPS() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveAPS()).getPreferencesId() > 0)) .visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveAPS() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveAPS()).getPreferencesId() > 0))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveAPS() != null) .validator(() -> ConfigBuilderPlugin.getPlugin().getActiveAPS() != null)
.visibility(() -> Config.APS) .visibility(() -> Config.APS);
)
.add(new SWScreen(R.string.apsmode_title) private SWScreen screenApsMode = new SWScreen(R.string.apsmode_title)
.skippable(false) .skippable(false)
.add(new SWRadioButton() .add(new SWRadioButton()
.option(R.array.aps_modeArray, R.array.aps_modeValues) .option(R.array.aps_modeArray, R.array.aps_modeValues)
.preferenceId(R.string.key_aps_mode).label(R.string.apsmode_title) .preferenceId(R.string.key_aps_mode).label(R.string.apsmode_title)
.comment(R.string.setupwizard_preferred_aps_mode)) .comment(R.string.setupwizard_preferred_aps_mode))
.validator(() -> SP.contains(R.string.key_aps_mode)) .validator(() -> SP.contains(R.string.key_aps_mode));
)
.add(new SWScreen(R.string.configbuilder_loop) private SWScreen screenLoop = new SWScreen(R.string.configbuilder_loop)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.setupwizard_loop_description)) .label(R.string.setupwizard_loop_description))
@ -389,9 +393,9 @@ public class SWDefinition {
}) })
.visibility(() -> !LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) .visibility(() -> !LoopPlugin.getPlugin().isEnabled(PluginType.LOOP)))
.validator(() -> LoopPlugin.getPlugin().isEnabled(PluginType.LOOP)) .validator(() -> LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))
.visibility(() -> !LoopPlugin.getPlugin().isEnabled(PluginType.LOOP) && Config.APS) .visibility(() -> !LoopPlugin.getPlugin().isEnabled(PluginType.LOOP) && Config.APS);
)
.add(new SWScreen(R.string.configbuilder_sensitivity) private SWScreen screenSensitivity = new SWScreen(R.string.configbuilder_sensitivity)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.setupwizard_sensitivity_description)) .label(R.string.setupwizard_sensitivity_description))
@ -406,34 +410,18 @@ public class SWDefinition {
.text(R.string.sensitivitysetup) .text(R.string.sensitivitysetup)
.action(() -> { .action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveSensitivity(); final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveSensitivity();
if (plugin != null) {
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> { PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class); Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId()); i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i); activity.startActivity(i);
}, null); }, null);
}
}) })
.visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveSensitivity() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveSensitivity()).getPreferencesId() > 0)) .visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveSensitivity() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveSensitivity()).getPreferencesId() > 0))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveSensitivity() != null) .validator(() -> ConfigBuilderPlugin.getPlugin().getActiveSensitivity() != null);
)
.add(new SWScreen(R.string.objectives) private SWScreen getScreenObjectives = new SWScreen(R.string.objectives)
.skippable(false)
.add(new SWInfotext()
.label(R.string.setupwizard_objectives_description))
.add(new SWButton()
.text(R.string.enableobjectives)
.action(() -> {
ObjectivesPlugin.getPlugin().setPluginEnabled(PluginType.CONSTRAINTS, true);
ObjectivesPlugin.getPlugin().setFragmentVisible(PluginType.CONSTRAINTS, true);
ConfigBuilderFragment.processOnEnabledCategoryChanged(ObjectivesPlugin.getPlugin(), PluginType.CONSTRAINTS);
ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard");
MainApp.bus().post(new EventConfigBuilderChange());
MainApp.bus().post(new EventSWUpdate(true));
})
.visibility(() -> !ObjectivesPlugin.getPlugin().isFragmentVisible()))
.validator(() -> ObjectivesPlugin.getPlugin().isEnabled(PluginType.CONSTRAINTS))
.visibility(() -> !ObjectivesPlugin.getPlugin().isFragmentVisible() && Config.APS)
)
.add(new SWScreen(R.string.objectives)
.skippable(false) .skippable(false)
.add(new SWInfotext() .add(new SWInfotext()
.label(R.string.startobjective)) .label(R.string.startobjective))
@ -441,197 +429,71 @@ public class SWDefinition {
.add(new SWFragment(this) .add(new SWFragment(this)
.add(new ObjectivesFragment())) .add(new ObjectivesFragment()))
.validator(() -> ObjectivesPlugin.getPlugin().objectives.get(0).isStarted()) .validator(() -> ObjectivesPlugin.getPlugin().objectives.get(0).isStarted())
.visibility(() -> !ObjectivesPlugin.getPlugin().objectives.get(0).isStarted() && Config.APS) .visibility(() -> !ObjectivesPlugin.getPlugin().objectives.get(0).isStarted() && Config.APS);
)
private void SWDefinitionFull() {
// List all the screens here
add(screenSetupWizard)
.add(screenLanguage)
.add(screenEula)
.add(screenPermissionBattery)
.add(screenPermissionBt)
.add(screenPermissionStore)
.add(screenImport)
.add(screenNsClient)
.add(screenAge)
.add(screenInsulin)
.add(screenBgSource)
.add(screenProfile)
.add(screenNsProfile)
.add(screenLocalProfile)
.add(screenSimpleProfile)
.add(screenProfileSwitch)
.add(screenPump)
.add(screenAps)
.add(screenApsMode)
.add(screenLoop)
.add(screenSensitivity)
.add(getScreenObjectives)
;
}
private void SWDefinitionPumpControl() {
// List all the screens here
add(screenSetupWizard)
.add(screenLanguage)
.add(screenEula)
.add(screenPermissionBattery)
.add(screenPermissionBt)
.add(screenPermissionStore)
.add(screenImport)
.add(screenNsClient)
.add(screenAge)
.add(screenInsulin)
.add(screenBgSource)
.add(screenProfile)
.add(screenNsProfile)
.add(screenLocalProfile)
.add(screenSimpleProfile)
.add(screenProfileSwitch)
.add(screenPump)
.add(screenSensitivity)
; ;
} }
private void SWDefinitionNSClient() { private void SWDefinitionNSClient() {
// List all the screens here // List all the screens here
add(new SWScreen(R.string.nav_setupwizard) add(screenSetupWizard)
.add(new SWInfotext() .add(screenLanguage)
.label(R.string.welcometosetupwizard)) .add(screenEula)
) .add(screenPermissionBattery)
.add(new SWScreen(R.string.language) .add(screenPermissionStore)
.skippable(false) .add(screenImport)
.add(new SWRadioButton() .add(screenNsClient)
.option(R.array.languagesArray, R.array.languagesValues) .add(screenBgSource)
.preferenceId(R.string.key_language).label(R.string.language) .add(screenAge)
.comment(R.string.setupwizard_language_prompt)) .add(screenInsulin)
.validator(() -> { .add(screenSensitivity)
String lang = SP.getString("language", "en");
LocaleHelper.setLocale(MainApp.instance().getApplicationContext(), lang);
return SP.contains(R.string.key_language);
})
)
.add(new SWScreen(R.string.end_user_license_agreement)
.skippable(false)
.add(new SWInfotext()
.label(R.string.end_user_license_agreement_text))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.end_user_license_agreement_i_understand)
.visibility(() -> !SP.getBoolean(R.string.key_i_understand, false))
.action(() -> {
SP.putBoolean(R.string.key_i_understand, true);
MainApp.bus().post(new EventSWUpdate(false));
}))
.visibility(() -> !SP.getBoolean(R.string.key_i_understand, false))
.validator(() -> SP.getBoolean(R.string.key_i_understand, false))
)
.add(new SWScreen(R.string.permission)
.skippable(false)
.add(new SWInfotext()
.label(String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name))))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.askforpermission)
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS))
.action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, AndroidPermission.CASE_BATTERY)))
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS))
.validator(() -> !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)))
)
.add(new SWScreen(R.string.permission)
.skippable(false)
.add(new SWInfotext()
.label(MainApp.gs(R.string.needstoragepermission)))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.askforpermission)
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE))
.action(() -> AndroidPermission.askForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, AndroidPermission.CASE_STORAGE)))
.visibility(() -> Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE))
.validator(() -> !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)))
)
.add(new SWScreen(R.string.nav_import)
.add(new SWInfotext()
.label(R.string.storedsettingsfound))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.nav_import)
.action(() -> ImportExportPrefs.importSharedPreferences(getActivity())))
.visibility(() -> ImportExportPrefs.file.exists() && !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !AndroidPermission.checkForPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)))
)
.add(new SWScreen(R.string.nsclientinternal_title)
.skippable(true)
.add(new SWInfotext()
.label(R.string.nsclientinfotext))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.enable_nsclient)
.action(() -> {
NSClientPlugin.getPlugin().setPluginEnabled(PluginType.GENERAL, true);
NSClientPlugin.getPlugin().setFragmentVisible(PluginType.GENERAL, true);
ConfigBuilderFragment.processOnEnabledCategoryChanged(NSClientPlugin.getPlugin(), PluginType.GENERAL);
ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard");
MainApp.bus().post(new EventConfigBuilderChange());
MainApp.bus().post(new EventSWUpdate(true));
})
.visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL)))
.add(new SWEditUrl()
.preferenceId(R.string.key_nsclientinternal_url)
.updateDelay(5)
.label(R.string.nsclientinternal_url_title)
.comment(R.string.nsclientinternal_url_dialogmessage))
.add(new SWEditString()
.validator(text -> text.length() >= 12)
.updateDelay(5)
.preferenceId(R.string.key_nsclientinternal_api_secret)
.label(R.string.nsclientinternal_secret_dialogtitle)
.comment(R.string.nsclientinternal_secret_dialogmessage))
.add(new SWBreak())
.add(new SWEventListener(this)
.label(R.string.status)
.initialStatus(NSClientPlugin.getPlugin().status)
.listener(new Object() {
@Subscribe
public void onEventNSClientStatus(EventNSClientStatus event) {
MainApp.bus().post(new EventSWLabel(event.status));
}
})
)
.add(new SWBreak())
.validator(() -> NSClientPlugin.getPlugin().nsClientService != null && NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth)
.visibility(() -> !(NSClientPlugin.getPlugin().nsClientService != null && NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth))
)
.add(new SWScreen(R.string.configbuilder_bgsource)
.skippable(false)
.add(new SWPlugin()
.option(PluginType.BGSOURCE, R.string.configbuilder_bgsource_description)
.label(R.string.configbuilder_bgsource))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.bgsourcesetup)
.action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveBgSource();
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i);
}, null);
})
.visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveBgSource()!= null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveBgSource()).getPreferencesId() > 0))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveBgSource() != null)
)
.add(new SWScreen(R.string.patientage)
.skippable(false)
.add(new SWInfotext()
.label(R.string.patientage_summary))
.add(new SWBreak())
.add(new SWRadioButton()
.option(R.array.ageArray, R.array.ageValues)
.preferenceId(R.string.key_age)
.label(R.string.patientage)
.comment(R.string.patientage_summary))
.validator(() -> SP.contains(R.string.key_age))
)
.add(new SWScreen(R.string.configbuilder_insulin)
.skippable(false)
.add(new SWPlugin()
.option(PluginType.INSULIN, R.string.configbuilder_insulin_description)
.makeVisible(false)
.label(R.string.configbuilder_insulin))
.add(new SWBreak())
.add(new SWInfotext()
.label(R.string.diawarning))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.insulinsourcesetup)
.action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveInsulin();
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i);
}, null);
})
.visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveInsulin()!= null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveInsulin()).getPreferencesId() > 0))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveInsulin() != null)
)
.add(new SWScreen(R.string.configbuilder_sensitivity)
.skippable(false)
.add(new SWInfotext()
.label(R.string.setupwizard_sensitivity_description))
.add(new SWHtmlLink()
.label(R.string.setupwizard_sensitivity_url))
.add(new SWBreak())
.add(new SWPlugin()
.option(PluginType.SENSITIVITY, R.string.configbuilder_sensitivity_description)
.label(R.string.configbuilder_sensitivity))
.add(new SWBreak())
.add(new SWButton()
.text(R.string.sensitivitysetup)
.action(() -> {
final PluginBase plugin = (PluginBase) ConfigBuilderPlugin.getPlugin().getActiveSensitivity();
PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(activity, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId());
activity.startActivity(i);
}, null);
})
.visibility(() -> ConfigBuilderPlugin.getPlugin().getActiveSensitivity() != null && ((PluginBase) ConfigBuilderPlugin.getPlugin().getActiveSensitivity()).getPreferencesId() > 0))
.validator(() -> ConfigBuilderPlugin.getPlugin().getActiveSensitivity() != null)
)
; ;
} }

View file

@ -3,6 +3,8 @@ package info.nightscout.androidaps.setupwizard;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -223,4 +225,10 @@ public class SetupWizardActivity extends AppCompatActivity {
updateButtons(); updateButtons();
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AndroidPermission.CASE_BATTERY)
updateButtons();
}
} }

View file

@ -1,10 +1,16 @@
package info.nightscout.androidaps.utils; package info.nightscout.androidaps.utils;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -21,33 +27,49 @@ public class AndroidPermission {
public static final int CASE_SMS = 0x2; public static final int CASE_SMS = 0x2;
public static final int CASE_LOCATION = 0x3; public static final int CASE_LOCATION = 0x3;
public static final int CASE_BATTERY = 0x4; public static final int CASE_BATTERY = 0x4;
public static final int CASE_PHONESTATE = 0x5; public static final int CASE_PHONE_STATE = 0x5;
public static void askForPermission(Activity activity, String[] permission, Integer requestCode) { @SuppressLint("BatteryLife")
private static void askForPermission(Activity activity, String[] permission, Integer requestCode) {
boolean test = false; boolean test = false;
for (int i = 0; i < permission.length; i++) { boolean testBattery = false;
test = test || (ContextCompat.checkSelfPermission(activity, permission[i]) != PackageManager.PERMISSION_GRANTED); for (String s : permission) {
test = test || (ContextCompat.checkSelfPermission(activity, s) != PackageManager.PERMISSION_GRANTED);
if (s.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
String packageName = activity.getPackageName();
testBattery = testBattery || !powerManager.isIgnoringBatteryOptimizations(packageName);
}
} }
if (test) { if (test) {
ActivityCompat.requestPermissions(activity, permission, requestCode); ActivityCompat.requestPermissions(activity, permission, requestCode);
} }
if (testBattery) {
Intent i = new Intent();
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + activity.getPackageName()));
activity.startActivityForResult(i, CASE_BATTERY);
}
} }
public static void askForPermission(Activity activity, String permission, Integer requestCode) { public static void askForPermission(Activity activity, String permission, Integer requestCode) {
String[] permissions = {permission}; String[] permissions = {permission};
askForPermission(activity, permissions, requestCode);
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(activity, permissions, requestCode);
} }
public static boolean checkForPermission(Context context, String permission) { public static boolean permissionNotGranted(Context context, String permission) {
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; boolean selfCheck = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
if (permission.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
String packageName = context.getPackageName();
selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName);
}
return !selfCheck;
} }
public static synchronized void notifyForSMSPermissions(Activity activity) { public static synchronized void notifyForSMSPermissions(Activity activity) {
if (SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)) { if (SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { if (permissionNotGranted(activity, Manifest.permission.RECEIVE_SMS)) {
if (!checkForPermission(activity, Manifest.permission.RECEIVE_SMS)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_SMS, MainApp.gs(R.string.smscommunicator_missingsmspermission), Notification.URGENT); NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_SMS, MainApp.gs(R.string.smscommunicator_missingsmspermission), Notification.URGENT);
notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.RECEIVE_SMS, notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.RECEIVE_SMS,
Manifest.permission.SEND_SMS, Manifest.permission.SEND_SMS,
@ -55,13 +77,12 @@ public class AndroidPermission {
MainApp.bus().post(new EventNewNotification(notification)); MainApp.bus().post(new EventNewNotification(notification));
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_SMS)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_SMS));
}
// Following is a bug in Android 8 // Following is a bug in Android 8
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
if (!checkForPermission(activity, Manifest.permission.READ_PHONE_STATE)) { if (permissionNotGranted(activity, Manifest.permission.READ_PHONE_STATE)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_PHONESTATE, MainApp.gs(R.string.smscommunicator_missingphonestatepermission), Notification.URGENT); NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_PHONESTATE, MainApp.gs(R.string.smscommunicator_missingphonestatepermission), Notification.URGENT);
notification.action(MainApp.gs(R.string.request), () -> notification.action(MainApp.gs(R.string.request), () ->
AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, AndroidPermission.CASE_PHONESTATE)); AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, AndroidPermission.CASE_PHONE_STATE));
MainApp.bus().post(new EventNewNotification(notification)); MainApp.bus().post(new EventNewNotification(notification));
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_PHONESTATE)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_PHONESTATE));
@ -70,19 +91,16 @@ public class AndroidPermission {
} }
public static synchronized void notifyForBatteryOptimizationPermission(Activity activity) { public static synchronized void notifyForBatteryOptimizationPermission(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
if (!checkForPermission(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_BATTERY, String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name)), Notification.URGENT); NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_BATTERY, String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name)), Notification.URGENT);
notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY)); notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY));
MainApp.bus().post(new EventNewNotification(notification)); MainApp.bus().post(new EventNewNotification(notification));
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_BATTERY)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_BATTERY));
} }
}
public static synchronized void notifyForStoragePermission(Activity activity) { public static synchronized void notifyForStoragePermission(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (!checkForPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT); NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT);
notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE)); Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE));
@ -90,16 +108,13 @@ public class AndroidPermission {
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_STORAGE)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_STORAGE));
} }
}
public static synchronized void notifyForLocationPermissions(Activity activity) { public static synchronized void notifyForLocationPermissions(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
if (!checkForPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_LOCATION, MainApp.gs(R.string.needlocationpermission), Notification.URGENT); NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_LOCATION, MainApp.gs(R.string.needlocationpermission), Notification.URGENT);
notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION)); notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION));
MainApp.bus().post(new EventNewNotification(notification)); MainApp.bus().post(new EventNewNotification(notification));
} else } else
MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_LOCATION)); MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_LOCATION));
} }
}
} }

View file

@ -1150,7 +1150,6 @@
<string name="adjustprofileinns">Changes must be done in NS</string> <string name="adjustprofileinns">Changes must be done in NS</string>
<string name="exitwizard">Skip setup wizard</string> <string name="exitwizard">Skip setup wizard</string>
<string name="setupwizard_loop_description">Press the button below to enable AndroidAPS to suggest/make basal changes</string> <string name="setupwizard_loop_description">Press the button below to enable AndroidAPS to suggest/make basal changes</string>
<string name="setupwizard_objectives_description">Press the button below to enable Objectives. Look in the Objectives tab, after you finish this wizard, to make AndroidAPS completely functional.\n</string>
<string name="enableobjectives">Enable Objectives</string> <string name="enableobjectives">Enable Objectives</string>
<string name="apssetup">Configure APS plugin</string> <string name="apssetup">Configure APS plugin</string>
<string name="key_setupwizard_processed" translatable="false">startupwizard_processed</string> <string name="key_setupwizard_processed" translatable="false">startupwizard_processed</string>