Skip to content

Commit

Permalink
Add function to inject wallpaper in home screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev4Mod committed May 11, 2024
1 parent d30a660 commit 5111321
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 31 deletions.
62 changes: 62 additions & 0 deletions app/src/main/java/com/wmods/wppenhacer/FilePicker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.wmods.wppenhacer;

import android.net.Uri;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.wmods.wppenhacer.utils.RealPathUtil;

import java.io.File;

public class FilePicker {

private static OnFilePickedListener mOnFilePickedListener;
private static AppCompatActivity mActivity;
public static ActivityResultLauncher<String> fileSalve;
private static OnUriPickedListener mOnUriPickedListener;
public static ActivityResultLauncher<String[]> fileCapture;

public static void registerFilePicker(AppCompatActivity activity) {
mActivity = activity;
fileCapture = activity.registerForActivityResult(new ActivityResultContracts.OpenDocument(), FilePicker::setFile);
fileSalve = activity.registerForActivityResult(new ActivityResultContracts.CreateDocument("*/*"), FilePicker::setFile);
}

private static void setFile(Uri uri) {
if (uri == null) return;

if (mOnFilePickedListener == null) {
mOnUriPickedListener.onUriPicked(uri);
mOnUriPickedListener = null;
}

if (mOnFilePickedListener != null) {
var realPath = RealPathUtil.getRealFilePath(mActivity, uri);
if (realPath == null) return;
mOnFilePickedListener.onFilePicked(new File(realPath));
mOnFilePickedListener = null;
}
}



public static void setOnFilePickedListener(OnFilePickedListener onFilePickedListener) {
mOnFilePickedListener = onFilePickedListener;
}

public static void setOnUriPickedListener(OnUriPickedListener onFilePickedListener) {
mOnUriPickedListener = onFilePickedListener;
}

public interface OnFilePickedListener {
void onFilePicked(File file);
}

public interface OnUriPickedListener {
void onUriPicked(Uri uri);
}

}
21 changes: 8 additions & 13 deletions app/src/main/java/com/wmods/wppenhacer/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package com.wmods.wppenhacer;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Window;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.wmods.wppenhacer.databinding.ActivityMainBinding;

import rikka.material.app.MaterialActivity;

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding binding;
Expand All @@ -42,11 +34,14 @@ protected void onCreate(Bundle savedInstanceState) {
NavController navController = navHostFragment.getNavController();
var nav = binding.navView;
NavigationUI.setupWithNavController(nav, navController);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_MEDIA_IMAGES}, 0);
}
FilePicker.registerFilePicker(this);
}

public static boolean isXposedEnabled() {
return false;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.wmods.wppenhacer.preference;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;

import com.wmods.wppenhacer.FilePicker;
import com.wmods.wppenhacer.R;

import java.io.File;
import java.util.Arrays;

public class FileSelectPreference extends Preference implements Preference.OnPreferenceClickListener, FilePicker.OnFilePickedListener {

private String[] mineTypes;

public FileSelectPreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context,attrs);
}

public FileSelectPreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}

public FileSelectPreference(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}

public void init(Context context, AttributeSet attrs) {
setOnPreferenceClickListener(this);
var typedArray = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.FileSelectPreference,
0, 0
);
var attrsArray = typedArray.getTextArray(R.styleable.FileSelectPreference_android_entryValues);
mineTypes = Arrays.stream(attrsArray).map(String::valueOf).toArray(String[]::new);
var prefs = PreferenceManager.getDefaultSharedPreferences(context);
var keyValue = prefs.getString(this.getKey(),null);
setSummary(keyValue);
}


@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
FilePicker.setOnFilePickedListener(this);
FilePicker.fileCapture.launch(mineTypes);
return false;
}

@Override
public void onFilePicked(File file) {
getSharedPreferences().edit().putString(getKey(),file.getAbsolutePath()).apply();
setSummary(file.getAbsolutePath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContract = registerForActivityResult(new ActivityResultContracts.OpenDocumentTree(), result -> {
if (result == null) return;
var realPath = RealPathUtil.getRealPathFromURI_API19(getContext(), result);
var realPath = RealPathUtil.getRealFolderPath(getContext(), result);
Preference preference = findPreference("localdownload");
if (preference != null) {
preference.setSummary(realPath);
Expand Down
70 changes: 69 additions & 1 deletion app/src/main/java/com/wmods/wppenhacer/utils/RealPathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,75 @@ public class RealPathUtil {
* @author paulburke
*/
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(final Context context, final Uri uri) {
public static String getRealFilePath(final Context context, final Uri uri) {

// DocumentProvider
if (DocumentsContract.isDocumentUri(context,uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}

// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {

final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));

return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];

Uri contentUri;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} else {
contentUri = MediaStore.Files.getContentUri("external");
}

final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};

return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {

// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();

return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}

return null;
}

@SuppressLint("NewApi")
public static String getRealFolderPath(final Context context, final Uri uri) {

// DocumentProvider
if (DocumentsContract.isTreeUri(uri)) {
Expand Down
54 changes: 54 additions & 0 deletions app/src/main/java/com/wmods/wppenhacer/views/WallpaperView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.wmods.wppenhacer.views;

import static de.robv.android.xposed.XposedBridge.log;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import java.io.File;

import de.robv.android.xposed.XSharedPreferences;

public class WallpaperView extends FrameLayout {
private final XSharedPreferences prefs;
private ImageView imageView;
private float mAlpha = 1f;

public WallpaperView(@NonNull Context context, XSharedPreferences preferences) {
super(context);
this.prefs = preferences;
init(context);
}

private void init(Context context) {
imageView = new android.widget.ImageView(context);
imageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(false);
try {
Bitmap bitmap = BitmapFactory.decodeFile(prefs.getString("wallpaper_file", ""));
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
this.mAlpha = (100 - prefs.getInt("wallpaper_alpha", 30)) / 100f;
addView(imageView);
}catch (Exception e){
log(e.toString());
}
}

@Override
public void addView(View child) {
if (child != imageView)
child.setAlpha(mAlpha);
super.addView(child);
}
}
Loading

0 comments on commit 5111321

Please sign in to comment.