rewrite preference screen

This commit is contained in:
Electric1447
2021-08-29 16:37:53 +03:00
parent 5cb5dac140
commit a700521fb5
8 changed files with 91 additions and 123 deletions
@@ -1,14 +1,14 @@
package com.dosse.airpods.notification;
import static com.dosse.airpods.notification.NotificationBuilder.NOTIFICATION_ID;
import static com.dosse.airpods.notification.NotificationBuilder.TAG;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import static com.dosse.airpods.notification.NotificationBuilder.NOTIFICATION_ID;
import static com.dosse.airpods.notification.NotificationBuilder.TAG;
import com.dosse.airpods.pods.PodsStatus;
import com.dosse.airpods.utils.Logger;
@@ -26,7 +26,7 @@ import com.dosse.airpods.utils.Logger;
public abstract class NotificationThread extends Thread {
private static final long SLEEP_TIMEOUT = 1000;
private final Context mContext;
private final String compat;
private final NotificationBuilder builder;
private final NotificationManager mNotifyManager;
@@ -35,8 +35,8 @@ public abstract class NotificationThread extends Thread {
public abstract PodsStatus getStatus ();
public NotificationThread (Context context) {
mContext = context;
mNotifyManager = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
compat = context.getPackageManager().getInstallerPackageName(context.getPackageName());
mNotifyManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
// On Oreo (API27) and newer, create a notification channel.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(TAG, TAG, NotificationManager.IMPORTANCE_LOW);
@@ -47,12 +47,11 @@ public abstract class NotificationThread extends Thread {
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotifyManager.createNotificationChannel(channel);
}
builder = new NotificationBuilder(mContext);
builder = new NotificationBuilder(context);
}
public void run () {
boolean notificationShowing = false;
String compat = mContext.getPackageManager().getInstallerPackageName(mContext.getPackageName());
while (!Thread.interrupted()) {
PodsStatus status = getStatus();
@@ -1,8 +1,8 @@
package com.dosse.airpods.pods;
import static com.dosse.airpods.pods.PodsStatusScanCallback.getScanFilters;
import static com.dosse.airpods.utils.SharedPreferencesUtils.isSavingBattery;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
@@ -17,14 +17,12 @@ import android.bluetooth.le.ScanSettings;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import android.os.ParcelUuid;
import android.provider.Settings;
import androidx.annotation.RequiresApi;
import androidx.preference.PreferenceManager;
import com.dosse.airpods.receivers.BluetoothListener;
import com.dosse.airpods.receivers.BluetoothReceiver;
@@ -46,7 +44,6 @@ public class PodsService extends Service {
private BluetoothLeScanner btScanner;
private PodsStatus status = PodsStatus.DISCONNECTED;
@SuppressLint("StaticFieldLeak")
private static NotificationThread n = null;
private static boolean maybeConnected = false;
@@ -67,8 +64,6 @@ public class PodsService extends Service {
try {
Logger.debug("START SCANNER");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean batterySaver = prefs.getBoolean("batterySaver", false);
BluetoothManager btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter btAdapter = btManager.getAdapter();
@@ -91,7 +86,7 @@ public class PodsService extends Service {
btScanner = btAdapter.getBluetoothLeScanner();
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(batterySaver ? ScanSettings.SCAN_MODE_LOW_POWER : ScanSettings.SCAN_MODE_LOW_LATENCY)
.setScanMode(isSavingBattery(getApplicationContext()) ? ScanSettings.SCAN_MODE_LOW_POWER : ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(1) // DON'T USE 0
.build();
@@ -230,9 +225,7 @@ public class PodsService extends Service {
Logger.error(t);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (prefs.getBoolean("batterySaver", false)) {
if (isSavingBattery(getApplicationContext())) {
screenReceiver = new ScreenReceiver() {
@Override
public void onStart () {
@@ -12,12 +12,6 @@ import com.dosse.airpods.R;
public class AboutActivity extends AppCompatActivity {
public static final String websiteURL = "https://fdossena.com/?p=openPods/index.frag";
public static final String githubURL = "https://github.com/adolfintel/OpenPods";
public static final String donateURL = "https://paypal.me/sineisochronic";
public static final String fdroidURL = "https://f-droid.org/packages/com.dosse.airpods/";
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -40,10 +34,10 @@ public class AboutActivity extends AppCompatActivity {
((WebView)(findViewById(R.id.license))).loadUrl("file:///android_asset/license.html");
findViewById(R.id.website).setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(websiteURL))));
findViewById(R.id.github).setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(githubURL))));
findViewById(R.id.website).setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.website_url)))));
findViewById(R.id.github).setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.github_url)))));
findViewById(R.id.donate).setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(donateURL)));
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.donate_url))));
Toast.makeText(AboutActivity.this, "❤️", Toast.LENGTH_SHORT).show();
});
@@ -1,19 +1,17 @@
package com.dosse.airpods.ui;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import com.dosse.airpods.BuildConfig;
import com.dosse.airpods.R;
import com.dosse.airpods.receivers.StartupReceiver;
import java.util.Objects;
public class SettingsActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
@@ -23,25 +21,36 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
.beginTransaction()
.replace(R.id.settings_container, new SettingsFragment())
.commit();
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
if (key.equalsIgnoreCase("batterySaver"))
StartupReceiver.restartPodsService(getApplicationContext());
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public boolean onOptionsItemSelected (@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
@Override
public void onCreatePreferences (Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference_screen, rootKey);
getPreference("batterySaver").setOnPreferenceClickListener(preference -> {
StartupReceiver.restartPodsService(requireContext());
return true;
});
getPreference("restartService").setOnPreferenceClickListener(preference -> {
StartupReceiver.restartPodsService(requireContext());
return true;
});
getPreference("about").setSummary(String.format("%s v%s", getString(R.string.app_name), BuildConfig.VERSION_NAME));
getPreference("donate").setOnPreferenceClickListener(preference -> {
Toast.makeText(requireContext(), "❤️", Toast.LENGTH_SHORT).show();
return false;
});
}
return false;
private Preference getPreference (String key) {
return getPreferenceManager().findPreference(key);
}
}
}
@@ -1,67 +0,0 @@
package com.dosse.airpods.ui;
import static com.dosse.airpods.ui.AboutActivity.donateURL;
import static com.dosse.airpods.ui.AboutActivity.fdroidURL;
import static com.dosse.airpods.ui.AboutActivity.githubURL;
import static com.dosse.airpods.ui.AboutActivity.websiteURL;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import com.dosse.airpods.BuildConfig;
import com.dosse.airpods.R;
import com.dosse.airpods.receivers.StartupReceiver;
import java.util.Objects;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences (Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference_screen, rootKey);
Preference mRestartPreference = getPreferenceManager().findPreference("restartService");
Objects.requireNonNull(mRestartPreference).setOnPreferenceClickListener(preference -> {
StartupReceiver.restartPodsService(requireContext());
return true;
});
Preference mAboutPreference = getPreferenceManager().findPreference("about");
Objects.requireNonNull(mAboutPreference).setSummary(String.format("%s v%s", getString(R.string.app_name), BuildConfig.VERSION_NAME));
mAboutPreference.setOnPreferenceClickListener(preference -> {
startActivity(new Intent(requireContext(), AboutActivity.class));
return true;
});
Preference mFDroidPreference = getPreferenceManager().findPreference("fdroid");
Objects.requireNonNull(mFDroidPreference).setOnPreferenceClickListener(preference -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(fdroidURL)));
return true;
});
Preference mGithubPreference = getPreferenceManager().findPreference("github");
Objects.requireNonNull(mGithubPreference).setOnPreferenceClickListener(preference -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(githubURL)));
return true;
});
Preference mWebsitePreference = getPreferenceManager().findPreference("website");
Objects.requireNonNull(mWebsitePreference).setOnPreferenceClickListener(preference -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(websiteURL)));
return true;
});
Preference mDonationPreference = getPreferenceManager().findPreference("donate");
Objects.requireNonNull(mDonationPreference).setOnPreferenceClickListener(preference -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(donateURL)));
Toast.makeText(requireContext(), "❤️", Toast.LENGTH_SHORT).show();
return true;
});
}
}
@@ -0,0 +1,15 @@
package com.dosse.airpods.utils;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
public class SharedPreferencesUtils {
public static boolean isSavingBattery (Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean("batterySaver", false);
}
}
@@ -50,4 +50,9 @@
<string name="website">Website</string>
<string name="github" tools:ignore="MissingTranslation">Github</string>
<string name="donate">Donate</string>
<string name="fdroid_url" translatable="false">https://f-droid.org/packages/com.dosse.airpods/</string>
<string name="website_url" translatable="false">https://fdossena.com/?p=openPods/index.frag</string>
<string name="github_url" translatable="false">https://github.com/adolfintel/OpenPods</string>
<string name="donate_url" translatable="false">https://paypal.me/sineisochronic</string>
</resources>
@@ -6,7 +6,7 @@
android:title="@string/app_name"
app:iconSpaceReserved="false">
<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:key="batterySaver"
android:summary="@string/batterySaver_desc"
@@ -29,27 +29,47 @@
android:key="about"
android:summary="@string/app_name"
android:title="@string/about"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false">
<intent
android:targetClass="com.dosse.airpods.ui.AboutActivity"
android:targetPackage="com.dosse.airpods" />
</Preference>
<Preference
android:key="fdroid"
android:title="@string/fdroid"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false">
<intent
android:action="android.intent.action.VIEW"
android:data="@string/fdroid_url" />
</Preference>
<Preference
android:key="website"
android:title="@string/website"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false">
<intent
android:action="android.intent.action.VIEW"
android:data="@string/website_url" />
</Preference>
<Preference
android:key="github"
android:title="@string/github"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false">
<intent
android:action="android.intent.action.VIEW"
android:data="@string/github_url" />
</Preference>
<Preference
android:key="donate"
android:title="@string/donate"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false">
<intent
android:action="android.intent.action.VIEW"
android:data="@string/donate_url" />
</Preference>
</PreferenceCategory>