Skip to content

Commit

Permalink
Merge pull request #903 from Mahmud0808/canary
Browse files Browse the repository at this point in the history
Added AI model selection list preference
  • Loading branch information
Mahmud0808 authored Oct 16, 2024
2 parents a6e2579 + 1c78e45 commit 5c46352
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.LocaleList;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -41,24 +42,26 @@

import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import sh.siava.pixelxpert.BuildConfig;
import sh.siava.pixelxpert.R;
import sh.siava.pixelxpert.databinding.SettingsActivityBinding;
import sh.siava.pixelxpert.ui.models.SearchPreferenceItem;
import sh.siava.pixelxpert.utils.MLKitSegmentor;
import sh.siava.pixelxpert.ui.fragments.HooksFragment;
import sh.siava.pixelxpert.ui.fragments.UpdateFragment;
import sh.siava.pixelxpert.ui.models.SearchPreferenceItem;
import sh.siava.pixelxpert.ui.preferences.preferencesearch.SearchConfiguration;
import sh.siava.pixelxpert.ui.preferences.preferencesearch.SearchPreference;
import sh.siava.pixelxpert.ui.preferences.preferencesearch.SearchPreferenceResult;
import sh.siava.pixelxpert.ui.preferences.preferencesearch.SearchPreferenceResultListener;
import sh.siava.pixelxpert.utils.AppUtils;
import sh.siava.pixelxpert.utils.ControlledPreferenceFragmentCompat;
import sh.siava.pixelxpert.utils.ExtendedSharedPreferences;
import sh.siava.pixelxpert.utils.MLKitSegmentor;
import sh.siava.pixelxpert.utils.NTPTimeSyncer;
import sh.siava.pixelxpert.utils.PrefManager;
import sh.siava.pixelxpert.utils.PreferenceHelper;
import sh.siava.pixelxpert.utils.PyTorchSegmentor;
import sh.siava.pixelxpert.utils.TimeSyncScheduler;
import sh.siava.pixelxpert.utils.UpdateScheduler;

Expand Down Expand Up @@ -477,19 +480,6 @@ public int getLayoutResource() {
}

public static class LockScreenFragment extends ControlledPreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
{
super.onCreatePreferences(savedInstanceState, rootKey);

new MLKitSegmentor(getActivity()).checkModelAvailability(moduleAvailabilityResponse ->
findPreference("DWallpaperEnabled")
.setSummary(
moduleAvailabilityResponse.areModulesAvailable()
? R.string.depth_wallpaper_model_ready
: R.string.depth_wallpaper_model_not_available));
}

@Override
public String getTitle() {
return getString(R.string.lockscreen_header_title);
Expand All @@ -504,7 +494,10 @@ public int getLayoutResource() {
public void updateScreen(String key) {
super.updateScreen(key);

if (key == null) return;
if (key == null) {
updateModelAvailabilitySummary();
return;
}

if (key.equals("DWallpaperEnabled")) {
try {
Expand All @@ -520,6 +513,29 @@ public void updateScreen(String key) {
}
} catch (Exception ignored) {
}
} else if (key.equals("SegmentorAI")) {
updateModelAvailabilitySummary();
}
}

private void updateModelAvailabilitySummary() {
try {
boolean mlKitModel = Integer.parseInt(mPreferences.getString("SegmentorAI", "0")) == 0;
AtomicBoolean modelDownloaded = new AtomicBoolean(false);

if (mlKitModel) {
new MLKitSegmentor(getActivity()).checkModelAvailability(moduleAvailabilityResponse ->
modelDownloaded.set(moduleAvailabilityResponse.areModulesAvailable()));
} else {
modelDownloaded.set(PyTorchSegmentor.loadAssets(getContext()));
}

findPreference("DWallpaperEnabled")
.setSummary(modelDownloaded.get()
? R.string.depth_wallpaper_model_ready
: R.string.depth_wallpaper_model_not_available);
} catch (Exception exception) {
Log.e(LockScreenFragment.class.getSimpleName(), exception.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public static boolean isVisible(String key) {
case "UpdateWifiOnly":
return instance.mPreferences.getBoolean("AutoUpdate", true);

case "SegmentorAI":
case "DWOpacity":
case "DWonAOD":
return instance.mPreferences.getBoolean("DWallpaperEnabled", false);
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@
<item>@string/thursday</item>
<item>@string/friday</item>
<item>@string/saturday</item>

</string-array>

<string-array name="days_of_week_codes">
Expand Down Expand Up @@ -562,4 +561,14 @@
<item>2</item>
</string-array>

<string-array name="segmentor_ai_model_names">
<item>ML Kit</item>
<item>Pytorch</item>
</string-array>

<string-array name="segmentor_ai_model_codes">
<item>0</item>
<item>1</item>
</string-array>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<!--Lock screen related-->
<string name="depth_wallpaper_title">Depth effect wallpaper</string>
<string name="depth_wallpaper_summary">Dynamically integrate wallpaper\'s foreground with the clock</string>
<string name="depth_wallpaper_ai_model_title">Preferred AI model</string>
<string name="depth_wallpaper_opacity_title">Wallpaper foreground opacity</string>
<string name="depth_wallpaper_aod_title">Also on \"always on display\"</string>
<string name="depth_wallpaper_model_ready">AI model is downloaded on the device</string>
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/res/xml/lock_screen_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@

<sh.siava.pixelxpert.ui.preferences.MaterialSwitchPreference
android:defaultValue="false"
app:allowDividerAbove="true"
android:key="DWallpaperEnabled"
android:summary="@string/depth_wallpaper_summary"
android:title="@string/depth_wallpaper_title"
app:allowDividerAbove="true"
app:iconSpaceReserved="false" />

<sh.siava.pixelxpert.ui.preferences.MaterialListPreference
android:defaultValue="0"
android:entries="@array/segmentor_ai_model_names"
android:entryValues="@array/segmentor_ai_model_codes"
android:key="SegmentorAI"
android:summary="%s"
android:title="@string/depth_wallpaper_ai_model_title"
app:iconSpaceReserved="false" />

<sh.siava.pixelxpert.ui.preferences.MaterialRangeSliderPreference
Expand All @@ -51,11 +60,11 @@

<sh.siava.pixelxpert.ui.preferences.MaterialSwitchPreference
android:defaultValue="false"
app:allowDividerAbove="true"
android:key="fingerprint_circle_hide"
android:summaryOff="@string/transparent_fingerprint_circle_off"
android:summaryOn="@string/transparent_fingerprint_circle_on"
android:title="@string/transparent_fingerprint_circle"
app:allowDividerAbove="true"
app:iconSpaceReserved="false" />

<sh.siava.pixelxpert.ui.preferences.MaterialSwitchPreference
Expand All @@ -68,11 +77,11 @@

<sh.siava.pixelxpert.ui.preferences.MaterialSwitchPreference
android:defaultValue="false"
app:allowDividerAbove="true"
android:key="DisableUnlockHintAnimation"
android:summaryOff="@string/general_off"
android:summaryOn="@string/general_on"
android:title="@string/disable_unlock_hint_title"
app:allowDividerAbove="true"
app:iconSpaceReserved="false" />

<sh.siava.pixelxpert.ui.preferences.MaterialSwitchPreference
Expand Down

0 comments on commit 5c46352

Please sign in to comment.