Skip to content

Commit

Permalink
Move Depth Wallpaper options in a new page
Browse files Browse the repository at this point in the history
Signed-off-by: DHD2280 <[email protected]>
  • Loading branch information
DHD2280 committed Dec 13, 2024
1 parent 264169c commit d34afcb
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package it.dhd.oxygencustomizer.ui.fragments.mods.lockscreen;

import static android.app.Activity.RESULT_OK;
import static it.dhd.oxygencustomizer.utils.Constants.ACTION_DEPTH_BACKGROUND_CHANGED;
import static it.dhd.oxygencustomizer.utils.Constants.ACTION_DEPTH_SUBJECT_CHANGED;
import static it.dhd.oxygencustomizer.utils.Constants.LOCKSCREEN_FINGERPRINT_FILE;
import static it.dhd.oxygencustomizer.utils.Constants.PLUGIN_URL;
import static it.dhd.oxygencustomizer.utils.Constants.Packages.SYSTEM_UI;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.Lockscreen.LOCKSCREEN_FINGERPRINT_STYLE;
import static it.dhd.oxygencustomizer.utils.Constants.getLockScreenBitmapCachePath;
import static it.dhd.oxygencustomizer.utils.Constants.getLockScreenSubjectCachePath;
import static it.dhd.oxygencustomizer.utils.FileUtil.getRealPath;
import static it.dhd.oxygencustomizer.utils.FileUtil.launchFilePicker;
import static it.dhd.oxygencustomizer.utils.FileUtil.moveToOCHiddenDir;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.preference.Preference;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import it.dhd.oneplusui.preference.OplusJumpPreference;
import it.dhd.oxygencustomizer.R;
import it.dhd.oxygencustomizer.ui.base.ControlledPreferenceFragmentCompat;
import it.dhd.oxygencustomizer.utils.AppUtils;
import it.dhd.oxygencustomizer.utils.BitmapSubjectSegmenter;

public class DepthWallpaperFragment extends ControlledPreferenceFragmentCompat {

private final int PICK_DEPTH_BACKGROUND = 1;
private final int PICK_DEPTH_SUBJECT = 2;
private int mPick = -1;
private OplusJumpPreference mAiStatus;

@Override
public String getTitle() {
return getString(R.string.depth_wallpaper_category);
}

@Override
public boolean backButtonEnabled() {
return true;
}

@Override
public int getLayoutResource() {
return R.xml.depth_wallpaper_prefs;
}

@Override
public boolean hasMenu() {
return true;
}

@Override
public String[] getScopes() {
return new String[]{SYSTEM_UI};
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);

Preference mDepthBackground = findPreference("DWBackground");
if (mDepthBackground != null) {
mDepthBackground.setOnPreferenceClickListener(preference -> {
mPick = PICK_DEPTH_BACKGROUND;
if (!AppUtils.hasStoragePermission()) {
AppUtils.requestStoragePermission(requireContext());
} else {
launchFilePicker(pickImageIntent, "image/*");
}
return true;
});
}

Preference mDepthSubject = findPreference("DWSubject");
if (mDepthSubject != null) {
mDepthSubject.setOnPreferenceClickListener(preference -> {
mPick = PICK_DEPTH_SUBJECT;
if (!AppUtils.hasStoragePermission()) {
AppUtils.requestStoragePermission(requireContext());
} else {
launchFilePicker(pickImageIntent, "image/*");
}
return true;
});
}
}

ActivityResultLauncher<Intent> pickImageIntent = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
String path = getRealPath(data);

String dest = switch (mPick) {
case PICK_DEPTH_BACKGROUND -> getLockScreenBitmapCachePath();
case PICK_DEPTH_SUBJECT -> getLockScreenSubjectCachePath();
default -> "";
};

if (path != null && moveToOCHiddenDir(path, dest)) {
switch (mPick) {
case PICK_DEPTH_BACKGROUND:
sendIntent(ACTION_DEPTH_BACKGROUND_CHANGED);
break;
case PICK_DEPTH_SUBJECT:
sendIntent(ACTION_DEPTH_SUBJECT_CHANGED);
break;
}
Toast.makeText(getContext(), requireContext().getResources().getString(R.string.toast_applied), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), requireContext().getResources().getString(R.string.toast_rename_file), Toast.LENGTH_SHORT).show();
}
}
});

private void sendIntent(String action) {
Intent intent = new Intent(action);
intent.putExtra("packageName", SYSTEM_UI);
requireContext().sendBroadcast(intent);
}

private void checkAiStatus() {
mAiStatus = findPreference("DWAIStatus");
if (mPreferences.getString("DWMode", "0").equals("0")) {
new BitmapSubjectSegmenter(getActivity()).checkModelAvailability(moduleAvailabilityResponse ->
mAiStatus
.setSummary(
moduleAvailabilityResponse.areModulesAvailable()
? R.string.depth_wallpaper_model_ready
: R.string.depth_wallpaper_model_not_available));
mAiStatus.setJumpEnabled(false);
} else if (mPreferences.getString("DWMode", "0").equals("2")) {
mAiStatus.setJumpEnabled(true);
if (AppUtils.isAppInstalled(requireContext(), "it.dhd.oxygencustomizer.aiplugin")) {
mAiStatus.setSummary(R.string.depth_wallpaper_plugin_installed);
mAiStatus.setOnPreferenceClickListener(preference -> {
Intent intent = requireContext().getPackageManager().getLaunchIntentForPackage("it.dhd.oxygencustomizer.aiplugin");
startActivity(intent);
return true;
});
} else {
mAiStatus.setSummary(R.string.depth_wallpaper_plugin_not_installed);
mAiStatus.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(PLUGIN_URL));
startActivity(intent);
return true;
});
}
}
}

@Override
public void updateScreen(String key) {
super.updateScreen(key);

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

switch (key) {
case "DWMode":
checkAiStatus();
break;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ public class Lockscreen extends ControlledPreferenceFragmentCompat {

private DateFormatDialog mDateFormatDialog;

private final int PICK_FP_ICON = 0;
private final int PICK_DEPTH_BACKGROUND = 1;
private final int PICK_DEPTH_SUBJECT = 2;
private int mPick = -1;

private OplusJumpPreference mAiStatus;

@Override
public String getTitle() {
return getString(R.string.lockscreen_title);
Expand Down Expand Up @@ -129,33 +122,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Preference mFingerprintPicker = findPreference("lockscreen_fp_icon_picker");
if (mFingerprintPicker != null) {
mFingerprintPicker.setOnPreferenceClickListener(preference -> {
mPick = PICK_FP_ICON;
if (!AppUtils.hasStoragePermission()) {
AppUtils.requestStoragePermission(requireContext());
} else {
launchFilePicker(pickImageIntent, "image/*");
}
return true;
});
}

Preference mDepthBackground = findPreference("DWBackground");
if (mDepthBackground != null) {
mDepthBackground.setOnPreferenceClickListener(preference -> {
mPick = PICK_DEPTH_BACKGROUND;
if (!AppUtils.hasStoragePermission()) {
AppUtils.requestStoragePermission(requireContext());
} else {
launchFilePicker(pickImageIntent, "image/*");
}
return true;
});
}

Preference mDepthSubject = findPreference("DWSubject");
if (mDepthSubject != null) {
mDepthSubject.setOnPreferenceClickListener(preference -> {
mPick = PICK_DEPTH_SUBJECT;
if (!AppUtils.hasStoragePermission()) {
AppUtils.requestStoragePermission(requireContext());
} else {
Expand Down Expand Up @@ -199,36 +165,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

}

private void checkAiStatus() {
mAiStatus = findPreference("DWAIStatus");
if (mPreferences.getString("DWMode", "0").equals("0")) {
new BitmapSubjectSegmenter(getActivity()).checkModelAvailability(moduleAvailabilityResponse ->
mAiStatus
.setSummary(
moduleAvailabilityResponse.areModulesAvailable()
? R.string.depth_wallpaper_model_ready
: R.string.depth_wallpaper_model_not_available));
mAiStatus.setJumpEnabled(false);
} else if (mPreferences.getString("DWMode", "0").equals("2")) {
mAiStatus.setJumpEnabled(true);
if (AppUtils.isAppInstalled(requireContext(), "it.dhd.oxygencustomizer.aiplugin")) {
mAiStatus.setSummary(R.string.depth_wallpaper_plugin_installed);
mAiStatus.setOnPreferenceClickListener(preference -> {
Intent intent = requireContext().getPackageManager().getLaunchIntentForPackage("it.dhd.oxygencustomizer.aiplugin");
startActivity(intent);
return true;
});
} else {
mAiStatus.setSummary(R.string.depth_wallpaper_plugin_not_installed);
mAiStatus.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(PLUGIN_URL));
startActivity(intent);
return true;
});
}
}
}

private void setJumps() {
OplusJumpPreference mLockscreenClock = findPreference("lockscreen_clock_main");
OplusJumpPreference mLockscreenWeather = findPreference("lockscreen_weather");
Expand Down Expand Up @@ -262,44 +198,20 @@ private void setJumps() {
Intent data = result.getData();
String path = getRealPath(data);

String dest = switch (mPick) {
case PICK_FP_ICON -> LOCKSCREEN_FINGERPRINT_FILE;
case PICK_DEPTH_BACKGROUND -> getLockScreenBitmapCachePath();
case PICK_DEPTH_SUBJECT -> getLockScreenSubjectCachePath();
default -> "";
};

if (path != null && moveToOCHiddenDir(path, dest)) {
switch (mPick) {
case PICK_FP_ICON:
mPreferences.edit().putString(LOCKSCREEN_FINGERPRINT_STYLE, "-1").apply();
break;
case PICK_DEPTH_BACKGROUND:
sendIntent(ACTION_DEPTH_BACKGROUND_CHANGED);
break;
case PICK_DEPTH_SUBJECT:
sendIntent(ACTION_DEPTH_SUBJECT_CHANGED);
break;
}
if (path != null && moveToOCHiddenDir(path, LOCKSCREEN_FINGERPRINT_FILE)) {
mPreferences.edit().putString(LOCKSCREEN_FINGERPRINT_STYLE, "-1").apply();
Toast.makeText(getContext(), requireContext().getResources().getString(R.string.toast_applied), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), requireContext().getResources().getString(R.string.toast_rename_file), Toast.LENGTH_SHORT).show();
}
}
});

private void sendIntent(String action) {
Intent intent = new Intent(action);
intent.putExtra("packageName", SYSTEM_UI);
requireContext().sendBroadcast(intent);
}

@Override
public void updateScreen(String key) {
super.updateScreen(key);

if (key == null) {
checkAiStatus();
setJumps();
return;
}
Expand All @@ -310,7 +222,7 @@ public void updateScreen(String key) {
boolean DepthEffectEnabled = mPreferences.getBoolean("DWallpaperEnabled", false);

if (DepthEffectEnabled) {
new MaterialAlertDialogBuilder(getContext())
new MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.depth_effect_alert_title)
.setMessage(getString(R.string.depth_effect_alert_body))
.setPositiveButton(R.string.depth_effect_ok_btn, (dialog, which) -> dialog.dismiss())
Expand All @@ -320,9 +232,6 @@ public void updateScreen(String key) {
} catch (Exception ignored) {
}
break;
case "DWMode":
checkAiStatus();
break;
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/it/dhd/oxygencustomizer/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,17 @@ public static class LockscreenWidgets {
public static final String LOCKSCREEN_WIDGETS_WEATHER_SETTINGS = "weather_settings";

}
public static class DepthWallpaper {
public static final String DEPTH_WALLPAPER_CATEGORY = "DWCategory";
public static final String DEPTH_WALLPAPER_ENABLED = "DWallpaperEnabled";
public static final String DEPTH_WALLPAPER_MODE = "DWMode";
public static final String DEPTH_WALLPAPER_AI_STATUS = "DWAIStatus";
public static final String DEPTH_WALLPAPER_OPACITY = "DWOpacity";
public static final String DEPTH_WALLPAPER_BACKGROUND = "DWBackground";
public static final String DEPTH_WALLPAPER_SUBJECT = "DWSubject";
public static final String DEPTH_WALLPAPER_AOD = "DWShowOnAod";
public static final String DEPTH_WALLPAPER_AOD_OPACITY = "DWAodOpacity";
}

// AOD
public static class AodClock {
Expand Down
Loading

0 comments on commit d34afcb

Please sign in to comment.