1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 18:45:49 +01:00

Add selection dialog to set OpenTracks package name

This commit is contained in:
vanous 2022-02-25 07:27:08 +01:00
parent 562856c3e2
commit 3ecc6519b0
3 changed files with 87 additions and 10 deletions

View File

@ -19,8 +19,11 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
@ -28,16 +31,19 @@ import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.provider.DocumentsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
@ -73,7 +79,8 @@ public class SettingsActivity extends AbstractSettingsActivity {
public static final String PREF_MEASUREMENT_SYSTEM = "measurement_system";
private static final int FILE_REQUEST_CODE = 4711;
private EditText fitnessAppEditText = null;
private int fitnessAppSelectionListSpinnerFirstRun = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -403,6 +410,70 @@ public class SettingsActivity extends AbstractSettingsActivity {
}
});
//fitness app (OpenTracks) package name selection for OpenTracks observer
pref = findPreference("pref_key_opentracks_packagename");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
final LinearLayout outerLayout = new LinearLayout(SettingsActivity.this);
outerLayout.setOrientation(LinearLayout.VERTICAL);
final LinearLayout innerLayout = new LinearLayout(SettingsActivity.this);
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
innerLayout.setPadding(20, 0, 20, 0);
final Spinner selectionListSpinner = new Spinner(SettingsActivity.this);
String[] appListArray = getResources().getStringArray(R.array.fitness_tracking_apps_package_names);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(SettingsActivity.this,
android.R.layout.simple_spinner_dropdown_item, appListArray);
selectionListSpinner.setAdapter(spinnerArrayAdapter);
fitnessAppSelectionListSpinnerFirstRun = 0;
addListenerOnSpinnerDeviceSelection(selectionListSpinner);
Prefs prefs = GBApplication.getPrefs();
String packageName = prefs.getString("opentracks_packagename", "de.dennisguse.opentracks");
fitnessAppEditText = new EditText(SettingsActivity.this);
fitnessAppEditText.setText(packageName);
innerLayout.addView(fitnessAppEditText);
outerLayout.addView(selectionListSpinner);
outerLayout.addView(innerLayout);
new AlertDialog.Builder(SettingsActivity.this)
.setCancelable(true)
.setTitle(R.string.pref_title_opentracks_packagename)
.setView(outerLayout)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = GBApplication.getPrefs().getPreferences().edit();
editor.putString("opentracks_packagename", fitnessAppEditText.getText().toString());
editor.apply();
editor.commit();
}
})
.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
return false;
}
});
}
private void addListenerOnSpinnerDeviceSelection(Spinner spinner) {
spinner.setOnItemSelectedListener(new SettingsActivity.CustomOnDeviceSelectedListener());
}
public class CustomOnDeviceSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (++fitnessAppSelectionListSpinnerFirstRun > 1) { //this prevents the setText to be set when spinner just is being initialized
fitnessAppEditText.setText(parent.getItemAtPosition(pos).toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
@Override

View File

@ -2109,5 +2109,13 @@
<item>mode_ask</item>
<item>mode_auto</item>
</string-array>
<string-array name="fitness_tracking_apps_package_names">
<item>de.dennisguse.opentracks</item>
<item>de.dennisguse.opentracks.playStore</item>
<item>de.dennisguse.opentracks.debug</item>
<item>de.dennisguse.opentracks.nightly</item>
</string-array>
</resources>

View File

@ -141,12 +141,10 @@
android:layout="@layout/preference_checkbox"
android:summary="@string/pref_summary_location_keep_uptodate"
android:title="@string/pref_title_location_keep_uptodate" />
<EditTextPreference
android:inputType="text"
android:key="opentracks_packagename"
android:defaultValue="de.dennisguse.opentracks"
android:title="@string/pref_title_opentracks_packagename"
android:summary="@string/pref_summary_opentracks_packagename" />
<Preference
android:key="pref_key_opentracks_packagename"
android:summary="@string/pref_summary_opentracks_packagename"
android:title="@string/pref_title_opentracks_packagename" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/preferences_category_device_specific_settings">