Skip to content

Commit

Permalink
CHANGELOG: Introduce Depth Wallpaper
Browse files Browse the repository at this point in the history
  • Loading branch information
DHD2280 committed May 8, 2024
1 parent 5deb876 commit 8fa257d
Show file tree
Hide file tree
Showing 15 changed files with 708 additions and 45 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ dependencies {
implementation("androidx.biometric:biometric:1.1.0")

// Material Design
implementation("com.google.android.material:material:1.12.0-beta01")
implementation("com.google.android.material:material:1.12.0")

// Preference
//noinspection KtxExtensionAvailable
implementation("androidx.preference:preference:1.2.1")
implementation("org.apache.commons:commons-text:${rootProject.extra["commonsTextVersion"]}")
// SwipeRefreshLayout
Expand Down Expand Up @@ -150,6 +151,10 @@ dependencies {
implementation("com.crossbowffs.remotepreferences:remotepreferences:0.8")

implementation("com.github.tiagohm.MarkdownView:library:0.19.0")

//Google Subject Segmentation - MLKit
implementation("com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1")
implementation("com.google.android.gms:play-services-base:18.4.0")
}

tasks.register("printVersionName") {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<meta-data
android:name="xposedscope"
android:resource="@array/xposed_scope" />
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="subject_segment" /> <!-- ML Kit download -->
<activity
android:name=".ui.activity.MainActivity"
android:exported="true"
Expand Down Expand Up @@ -98,7 +101,7 @@

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface IRootProviderProxy {
* and return values in AIDL.
*/
String[] runCommand(String command);
void extractSubject(in Bitmap input, String resultPath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

Expand All @@ -19,6 +20,7 @@

import it.dhd.oxygencustomizer.IRootProviderProxy;
import it.dhd.oxygencustomizer.R;
import it.dhd.oxygencustomizer.utils.BitmapSubjectSegmenter;

public class RootProviderProxy extends Service {
@Nullable
Expand All @@ -43,7 +45,7 @@ private RootPoviderProxyIPC(Context context)
catch (Throwable ignored){}
rootGranted = Shell.getShell().isRoot();

rootAllowedPacks = Arrays.asList(context.getResources().getStringArray(R.array.xposed_scope));
rootAllowedPacks = Arrays.asList(context.getResources().getStringArray(R.array.root_requirement));
}

/** @noinspection RedundantThrows*/
Expand All @@ -61,6 +63,44 @@ public String[] runCommand(String command) throws RemoteException {
}
}

@Override
public void extractSubject(Bitmap input, String resultPath) throws RemoteException {
ensureEnvironment();

try {
new BitmapSubjectSegmenter(getApplicationContext()).segmentSubject(input, new BitmapSubjectSegmenter.SegmentResultListener() {
@Override
public void onSuccess(Bitmap result) {
try {
File tempFile = File.createTempFile("lswt", ".png");

Log.d(TAG,"DepthWallpaper extractSubject: " + tempFile.getAbsolutePath() + " -> " + resultPath);

FileOutputStream outputStream = new FileOutputStream(tempFile);
result.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

outputStream.close();
result.recycle();

Shell.cmd("cp -F " + tempFile.getAbsolutePath() + " " + resultPath).exec();
Shell.cmd("chmod 644 " + resultPath).exec();
Log.d(TAG, "DepthWallpaper onSuccess: BitmapSubjectSegmenter " + resultPath);
Log.d(TAG, "DepthWallpaper onSuccess: BitmapSubjectSegmenter");
} catch (Throwable t) {
Log.e(TAG, "onSuccess: BitmapSubjectSegmenter", t);
}
}

@Override
public void onFail() {
Log.d(TAG, "onFail: BitmapSubjectSegmenter");
}
});
} catch (Throwable t) {
Log.e(TAG, "extractSubject: BitmapSubjectSegmenter", t);
}
}

private void ensureEnvironment() throws RemoteException {
if(!rootGranted)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public View onCreateView(

private void installApk(String downloadPath) {
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(
FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", new File(downloadPath)),
FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", new File(downloadPath)),
"application/vnd.android.package-archive");
promptInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import it.dhd.oxygencustomizer.ui.base.ControlledPreferenceFragmentCompat;
import it.dhd.oxygencustomizer.ui.models.ClockModel;
import it.dhd.oxygencustomizer.utils.AppUtils;
import it.dhd.oxygencustomizer.utils.BitmapSubjectSegmenter;
import it.dhd.oxygencustomizer.utils.CarouselLayoutManager;
import it.dhd.oxygencustomizer.utils.Constants;
import it.dhd.oxygencustomizer.utils.PreferenceHelper;
Expand Down Expand Up @@ -92,6 +93,8 @@
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreferenceCompat;

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

public class Lockscreen extends ControlledPreferenceFragmentCompat {
@Override
public String getTitle() {
Expand Down Expand Up @@ -185,6 +188,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
if (hideStatusbar != null) {
hideStatusbar.setOnPreferenceChangeListener(listener);
}

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

}

ActivityResultLauncher<Intent> startActivityIntent = registerForActivityResult(
Expand All @@ -203,6 +214,31 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}
});

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

if (key == null) return;

switch (key) {
case "DWallpaperEnabled":
try {
boolean DepthEffectEnabled = mPreferences.getBoolean("DWallpaperEnabled", false);

if (DepthEffectEnabled) {
new MaterialAlertDialogBuilder(getContext(), R.style.MaterialComponents_MaterialAlertDialog)
.setTitle(R.string.depth_effect_alert_title)
.setMessage(getString(R.string.depth_effect_alert_body, getString(R.string.sysui_restart_needed)))
.setPositiveButton(R.string.depth_effect_ok_btn, (dialog, which) -> AppUtils.restartScope("systemui"))
.setCancelable(false)
.show();
}
} catch (Exception ignored) {
}
break;
}
}

public static class LockscreenClock extends ControlledPreferenceFragmentCompat {
@Override
public String getTitle() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package it.dhd.oxygencustomizer.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;

import com.google.android.gms.common.moduleinstall.ModuleAvailabilityResponse;
import com.google.android.gms.common.moduleinstall.ModuleInstall;
import com.google.android.gms.common.moduleinstall.ModuleInstallClient;
import com.google.android.gms.common.moduleinstall.ModuleInstallRequest;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.mlkit.vision.common.InputImage;
import com.google.mlkit.vision.segmentation.subject.SubjectSegmentation;
import com.google.mlkit.vision.segmentation.subject.SubjectSegmenter;
import com.google.mlkit.vision.segmentation.subject.SubjectSegmenterOptions;

import java.nio.FloatBuffer;

public class BitmapSubjectSegmenter {
final SubjectSegmenter mSegmenter;
final Context mContext;
public BitmapSubjectSegmenter(Context context)
{
mContext = context;
mSegmenter = SubjectSegmentation.getClient(
new SubjectSegmenterOptions.Builder()
.enableForegroundConfidenceMask()
.build());

downloadModelIfNeeded();
}

public void downloadModelIfNeeded()
{
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(mContext);

moduleInstallClient
.areModulesAvailable(mSegmenter)
.addOnSuccessListener(
response -> {
if (!response.areModulesAvailable()) {
moduleInstallClient
.installModules(
ModuleInstallRequest.newBuilder()
.addApi(mSegmenter)
.build());
}
});
}

public void checkModelAvailability(OnSuccessListener<ModuleAvailabilityResponse> resultListener)
{
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(mContext);

moduleInstallClient.areModulesAvailable(mSegmenter).addOnSuccessListener(resultListener);
}

public void segmentSubject(Bitmap inputBitmap, SegmentResultListener listener)
{
int transparentColor = Color.alpha(Color.TRANSPARENT);

Bitmap resultBitmap = inputBitmap.copy(Bitmap.Config.ARGB_8888, true);

mSegmenter.process(InputImage.fromBitmap(inputBitmap, 0))
.addOnSuccessListener(subjectSegmentationResult -> {
FloatBuffer mSubjectMask = subjectSegmentationResult.getForegroundConfidenceMask();

resultBitmap.setHasAlpha(true);
for(int y = 0; y < inputBitmap.getHeight(); y++)
{
for(int x = 0; x < inputBitmap.getWidth(); x++)
{
//noinspection DataFlowIssue
if(mSubjectMask.get() < .5f)
{
resultBitmap.setPixel(x, y, transparentColor);
}
}
}

inputBitmap.recycle();
listener.onSuccess(resultBitmap);
})
.addOnFailureListener(e -> {
inputBitmap.recycle();
listener.onFail();
});

}

public interface SegmentResultListener{
void onSuccess(Bitmap result);
void onFail();
}
}
Loading

0 comments on commit 8fa257d

Please sign in to comment.