增加开发者模式

This commit is contained in:
2022-06-28 10:41:15 +08:00
parent b410754600
commit 964a1a8274
20 changed files with 1316 additions and 80 deletions

View File

@@ -63,9 +63,10 @@ class KotlinMainActivity : AppCompatActivity(), CancelAdapt {
val test = noteAidlInterface.note
Log.e("test", test)
}
}
}

View File

@@ -10,6 +10,7 @@ import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
@@ -30,6 +31,7 @@ import com.tt.ttutils.server.main.MainService;
import com.tt.ttutils.R;
import com.tt.ttutils.utils.java.CmdUtil;
import com.tt.ttutils.utils.java.JGYUtils;
import com.tt.ttutils.utils.java.TTUtils;
import com.tt.ttutils.utils.java.ToastUtil;
import com.tt.ttutils.utils.jni.NDKTools;
@@ -40,6 +42,12 @@ import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends ImmerseBaseActivity {
@BindView(R.id.tv_qch_Developeroptions)
TextView tv_qch_Developeroptions;
@BindView(R.id.tv_development_settings_enabled)
TextView tv_development_settings_enabled;
@BindView(R.id.tv_adb_enabled)
TextView tv_adb_enabled;
@BindView(R.id.textView)
TextView textView;
@BindView(R.id.button)
@@ -78,6 +86,16 @@ public class MainActivity extends ImmerseBaseActivity {
@BindView(R.id.bt_start_jgy)
Button bt_start_jgy;
@BindView(R.id.tv_statu)
TextView tv_statu;
@BindView(R.id.bt_camera_close)
Button bt_camera_close;
@BindView(R.id.bt_camera_open)
Button bt_camera_open;
@BindView(R.id.bt_camera_refresh)
Button bt_camera_refresh;
private ServiceConnection mServiceConnection;
private INoteAidlInterface noteAidlInterface;
private ServiceConnection mLicenseServiceConnection;
@@ -105,10 +123,21 @@ public class MainActivity extends ImmerseBaseActivity {
// int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION //导航栏
// | View.SYSTEM_UI_FLAG_FULLSCREEN;//全屏
// decorView.setSystemUiVisibility(uiOptions);
Intent intent = getIntent();
if (intent != null) {
TTUtils.getExtrax(intent);
}
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent != null) {
TTUtils.getExtrax(intent);
}
}
@Override
public void setListener() {
// mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_NONE, true);
@@ -309,6 +338,41 @@ public class MainActivity extends ImmerseBaseActivity {
// }
}
});
bt_camera_refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int code = Settings.System.getInt(getContentResolver(), "qch_app_camera", 0);
tv_statu.setText("状态:" + code);
}
});
bt_camera_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.System.putInt(getContentResolver(), "qch_app_camera", 1);
Intent cameraIntent = new Intent("qch_camera_forbid").setPackage("com.android.settings");
sendBroadcast(cameraIntent);
}
});
bt_camera_open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.System.putInt(getContentResolver(), "qch_app_camera", 0);
Intent cameraIntent = new Intent("qch_camera_open").setPackage("com.android.settings");
sendBroadcast(cameraIntent);
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume: " + Settings.System.getInt(getContentResolver(), "qch_Developeroptions", 0));
tv_qch_Developeroptions.setText("qch_Developeroptions: " + Settings.System.getInt(getContentResolver(), "qch_Developeroptions", 0));
Log.e(TAG, "onResume: " + Settings.Global.getInt(getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0));
tv_development_settings_enabled.setText("development_settings_enabled: " + Settings.Global.getInt(getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0));
Log.e(TAG, "onResume: " + Settings.Global.getInt(getContentResolver(), Settings.Global.ADB_ENABLED, 0));
tv_adb_enabled.setText("development_settings_enabled: " + Settings.Global.getInt(getContentResolver(), Settings.Global.ADB_ENABLED, 0));
}
private HideAppReceiver hideAppReceiver;

View File

@@ -0,0 +1,102 @@
package com.tt.ttutils.activity.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.tt.ttutils.R;
import com.tt.ttutils.utils.java.TTUtils;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class TestActivity extends AppCompatActivity {
private String TAG = TestActivity.class.getSimpleName();
@BindView(R.id.bt1)
Button bt1;
@BindView(R.id.bt2)
Button bt2;
@BindView(R.id.bt3)
Button bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ButterKnife.bind(this);
Intent intent = getIntent();
if (intent != null) {
TTUtils.getExtrax(intent);
}
bt1.setOnClickListener(view -> setDefault("com.aoleyunos.dop1"));
bt2.setOnClickListener(view -> setDefault("com.aoleyunos.dop2"));
bt3.setOnClickListener(view -> setDefault("com.aoleyun.os"));
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent != null) {
TTUtils.getExtrax(intent);
}
}
private void setDefault(String pkg) {
String className = getStartClassName(pkg);
Intent intent = new Intent("setDefaultLauncher");
intent.putExtra("package", pkg);
intent.putExtra("className", className);
//爱华定制
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.AoleReceiver"));
intent.setPackage("com.android.settings");
sendBroadcast(intent);
}
public String getStartClassName(String pkg) {
PackageInfo packageInfo = null;
try {
packageInfo = getPackageManager().getPackageInfo(pkg, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo == null) {
return "";
} else {
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageInfo.packageName);
// 通过getPackageManager()的queryIntentActivities方法遍历
List<ResolveInfo> resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
if (resolveinfoList == null || resolveinfoList.size() == 0) {
return "";
}
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
// packagename = 参数packname
String packageName = resolveinfo.activityInfo.packageName;
// 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式packagename.mainActivityname]
String className = resolveinfo.activityInfo.name;
return className;
} else {
return "";
}
}
}
}

View File

@@ -0,0 +1,45 @@
package com.tt.ttutils.gson;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
public class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<T> rawType = (Class<T>) type.getRawType();
if (rawType != String.class) {
return null;
}
return (TypeAdapter<T>) new StringAdapter();
}
public static class StringAdapter extends TypeAdapter<String> {
@Override
public String read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return "";
}
return reader.nextString();
}
@Override
public void write(JsonWriter writer, String value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.value(value);
}
}
}

View File

@@ -0,0 +1,75 @@
package com.tt.ttutils.network.interceptor;
import android.util.Log;
import com.tt.ttutils.utils.java.MD5Util;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import okhttp3.Interceptor;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okio.Buffer;
public class RepeatRequestInterceptor implements Interceptor {
private static final String TAG = RepeatRequestInterceptor.class.getSimpleName();
private final ConcurrentHashMap<String, Long> requestIdsMap = new ConcurrentHashMap<>();
public static final String REPEAT_REQUEST_PROTOCOL = "OKHTTP_REPEAT_REQUEST_PROTOCOL";
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(chain.request());
String content = response.body().string();
// Log.e(TAG, "请求体返回:| Response:" + content);
//相同的请求
String requestKey = MD5Util.getUpperMD5Str(request.method() + request.url().toString() + request.body());
long time = System.currentTimeMillis();//请求时间
try {
if (requestIdsMap.size() > 0 && requestIdsMap.containsKey(requestKey)) {
// Log.e("REPEAT-REQUEST", "重复请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url() + "\t" + bodyToString(request));
Log.e("REPEAT-REQUEST", "重复请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url());
//下面这行写了不会抛出onerror
// chain.call().cancel();
return new Response.Builder()
.protocol(Protocol.get(REPEAT_REQUEST_PROTOCOL))
.request(request) //multi thread
.build();
}
requestIdsMap.put(requestKey, time);
// Log.e("REPEAT-REQUEST", "注册请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url() + "\t" + bodyToString(request));
Log.e("REPEAT-REQUEST", "注册请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url());
// RepeatRequestInterceptor.Builder builder = request.newBuilder();
// builder.addHeader("header", jsonObject.toString());
return chain.proceed(request);
} catch (IOException e) {
Log.e(TAG, "intercept: " + e.getMessage());
throw e;
} finally {
if (requestIdsMap.containsKey(requestKey) && requestIdsMap.containsValue(time)) {//请求任务完成删除map中的数据
requestIdsMap.remove(requestKey);
Log.e("REPEAT-REQUEST", "移除请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url() + "\t" + bodyToString(request));
// Log.e("REPEAT-REQUEST", "移除请求:" + requestKey + " Method @" + request.method() + " --- " + " URL = " + request.url());
}
}
}
private static String bodyToString(final Request request) {
try {
final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
return buffer.readUtf8();
} catch (Exception e) {
return "-";
}
}
}

View File

@@ -1,15 +1,107 @@
package com.tt.ttutils.utils.java;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.IPackageDataObserver;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import android.content.pm.PackageStats;
import android.os.RemoteException;
import android.util.Log;
import java.util.List;
import java.io.File;
import java.lang.reflect.Method;
public class ApkUtils {
private static final String TAG = ApkUtils.class.getSimpleName();
private final long MAX_WAIT_TIME = 60 * 1000;
private final long WAIT_TIME_INCR = 10 * 1000;
/**
* 通过包名获取大小
*
* @param context
* @param packageName
* @return
*/
public static long getPackageSize(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = packageManager.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo == null) {
return 0L;
} else {
return new File(packageInfo.applicationInfo.publicSourceDir).length();
}
}
public long getApplicationCache(Context context, String packageName) throws Exception {
try {
PackageStatsObserver observer = new PackageStatsObserver();
// wait on observer
synchronized (observer) {
Method method = PackageManager.class.getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
method.invoke(context.getPackageManager(), packageName, observer);
// getPm().getPackageSizeInfo(packageName, UserHandle.myUserId(), observer);
long waitTime = 0;
while ((!observer.isDone()) || (waitTime > MAX_WAIT_TIME)) {
observer.wait(WAIT_TIME_INCR);
waitTime += WAIT_TIME_INCR;
}
if (!observer.isDone()) {
throw new Exception("Timed out waiting for PackageStatsObserver.onGetStatsCompleted");
}
}
Log.d(TAG, "toString " + observer.stats.toString());
return observer.stats.dataSize + observer.stats.cacheSize;
} catch (RemoteException e) {
Log.w(TAG, "Failed to get handle for PackageManger Exception: " + e);
return -1;
} catch (InterruptedException e) {
Log.w(TAG, "InterruptedException :" + e);
return -1;
}
}
static class PackageDataObserver extends IPackageDataObserver.Stub {
public boolean retValue = false;
private boolean doneFlag = false;
public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException {
synchronized (this) {
retValue = succeeded;
doneFlag = true;
notifyAll();
}
}
public boolean isDone() {
return doneFlag;
}
}
static class PackageStatsObserver extends IPackageStatsObserver.Stub {
boolean retValue = false;
PackageStats stats;
private boolean doneFlag = false;
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
synchronized (this) {
retValue = succeeded;
stats = pStats;
doneFlag = true;
notifyAll();
}
}
public boolean isDone() {
return doneFlag;
}
}
}

View File

@@ -23,6 +23,7 @@ public class AppUtils {
}
//https://www.jianshu.com/p/9dac8c2aeb4d
//https://www.jianshu.com/p/33695ee91d18
public static boolean openPackage(Context context, String packageName) {
Context pkgContext = getPackageContext(context, packageName);

View File

@@ -131,5 +131,4 @@ public class DevicesUtils {
}
return jsonObject.toString();
}
}

View File

@@ -5,8 +5,11 @@ import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import com.tt.ttutils.BuildConfig;
import com.tt.ttutils.disklrucache.CacheHelper;
import com.tt.ttutils.receiver.BootReceiver;
@@ -20,6 +23,19 @@ public class JGYUtils {
private CacheHelper cacheHelper;
public static int UnknowPlatform = 0;
public static int MTKPlatform = 1;
// TODO: 2022/4/23 标签替换未完成
public static int CubePlatform = 2;
public static int ZhanruiPlatform = 3;
public static String Other = "其他";
public static String MTKTag = "MTK";
// TODO: 2022/4/23 标签替换未完成
public static String CubeTag = "展锐cube";
public static String ZhanruiTag = "展锐";
private JGYUtils(Context context) {
if (context == null) {
throw new RuntimeException("Context is NULL");
@@ -43,6 +59,117 @@ public class JGYUtils {
return sInstance;
}
public int checkSNPlatform(String sn) {
String secondChars = sn.substring(1, 2);
if ("N".equalsIgnoreCase(secondChars)) {//MTK平台
return MTKPlatform;
} else if ("R".equalsIgnoreCase(secondChars)) {//展锐平台
return ZhanruiPlatform;
} else {
Log.e(TAG, "checkSNPlatform: " + "sn: " + sn + "没有对应平台");
return UnknowPlatform;
}
}
public int checkAppPlatform() {
String platform = BuildConfig.platform;
if ("MTK".equalsIgnoreCase(platform)) {
Log.i(TAG, "checkAppPlatform: " + "MTK平台");
return MTKPlatform;
} else if ("ZhanRui".equalsIgnoreCase(platform)) {
Log.i(TAG, "checkAppPlatform: " + "展锐平台");
return ZhanruiPlatform;
} else if ("ZhanRuiCube".equalsIgnoreCase(platform)) {
Log.i(TAG, "checkAppPlatform: " + "酷比平台");
return CubePlatform;
} else {
Log.i(TAG, "checkAppPlatform: " + "没有数据");
return UnknowPlatform;
}
}
public boolean isSamePlatform(String platform) {
String AppPlatform = BuildConfig.platform;
if ("ZhanRui".equals(AppPlatform)) {
return ZhanruiTag.equals(platform);
} else {
return AppPlatform.equals(platform);
}
}
public interface GetAppPlatformCallback {
void AppPlatform(int platform);
}
public void getAppPlatform(GetAppPlatformCallback getAppPlatformCallback) {
String platform = BuildConfig.platform;
if ("MTK".equalsIgnoreCase(platform)) {
getAppPlatformCallback.AppPlatform(MTKPlatform);
} else if ("ZhanRui".equalsIgnoreCase(platform)) {
getAppPlatformCallback.AppPlatform(ZhanruiPlatform);
} else if ("ZhanRuiCube".equalsIgnoreCase(platform)) {
getAppPlatformCallback.AppPlatform(CubePlatform);
} else {
getAppPlatformCallback.AppPlatform(UnknowPlatform);
}
}
public String getAppPlatform() {
String platform = BuildConfig.platform;
if ("MTK".equalsIgnoreCase(platform)) {
return MTKTag;
} else if ("ZhanRui".equalsIgnoreCase(platform)) {
return ZhanruiTag;
} else if ("ZhanRuiCube".equalsIgnoreCase(platform)) {
return CubeTag;
} else {
return Other;
}
}
/**
* 从Manifest中获取meta-data值
* https://blog.csdn.net/yue_233/article/details/91453451
*
* @return
*/
public String getStringMetaData() {
ApplicationInfo appInfo = null;
try {
appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String value = appInfo.metaData.getString("CHANNEL_VALUE");
return value;
}
public static boolean isOfficialVersion() {
String channelValue = JGYUtils.getInstance().getStringMetaData();
return "official".equals(channelValue);
}
public static boolean isOfficialVersion(Context context) {
Log.e(TAG, "isOfficialVersion: " + ProcessUtil.getCurrentProcessName(context));
String channelValue = JGYUtils.getInstance().getStringMetaData();
return "official".equals(channelValue);
}
public static boolean isNewlyVersion() {
String channelValue = JGYUtils.getInstance().getStringMetaData();
return "beta".equals(channelValue);
}
public static boolean isBetaVersion() {
String channelValue = JGYUtils.getInstance().getStringMetaData();
return "beta".equals(channelValue);
}
public static String getCHANNEL_VALUE() {
return JGYUtils.getInstance().getStringMetaData();
}
public static final String PACKAGE_DEVICEINFO = "com.aoleyun.sn";
public static final String CLASS_DEVICEINFO = "com.aoleyun.sn.receiver.BootReceiver";

View File

@@ -0,0 +1,175 @@
package com.tt.ttutils.utils.java;
import android.app.Activity;
import android.app.WallpaperManager;
import android.app.role.RoleManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Process;
import android.os.UserHandle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import com.tt.ttutils.R;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
public class LauncherUtils {
public static void setRoleHolderAsUser(Context context, String packageName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
String roleName = "android.app.role.HOME";
boolean add = true;
int flags = 0;
UserHandle user = Process.myUserHandle();
Log.i("settingssssssstemf", (add ? "Adding" : "Removing") + " package as role holder, role: "
+ roleName + ", package: " + packageName);
if (JGYUtils.getInstance().checkAppPlatform() != JGYUtils.MTKPlatform) {
RoleManager roleManager = context.getSystemService(RoleManager.class);
Executor executor = context.getMainExecutor();
Consumer<Boolean> callback = successful -> {
if (successful) {
Log.d("settingssssssstemf", "Package " + (add ? "added" : "removed")
+ " as role holder, role: " + roleName + ", package: " + packageName);
} else {
Log.d("settingssssssstemf", "Failed to " + (add ? "add" : "remove")
+ " package as role holder, role: " + roleName + ", package: "
+ packageName);
}
};
roleManager.addRoleHolderAsUser(roleName, packageName, flags, user, executor, callback);
Log.i("settingssssssstemf", "addRoleHolderAsUser done");
}
} else {
}
}
// private void setDefault() {
// String mRoleName ="android.app.role.HOME";
// final Intent intent = new Intent(Intent.ACTION_MANAGE_DEFAULT_APP)
// .putExtra(Intent.EXTRA_ROLE_NAME, mRoleName);
// startActivity(intent);
// }
public static boolean isDefaultHome(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN);//Intent.ACTION_VIEW
intent.addCategory("android.intent.category.HOME");
intent.addCategory("android.intent.category.DEFAULT");
PackageManager pm = context.getPackageManager();
ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
boolean isDefault = context.getPackageName().equals(info.activityInfo.packageName);
return isDefault;
}
private String getHomeLauncher(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, 0);
String currentHomePackage = resolveInfo.activityInfo.packageName;
return currentHomePackage;
}
private void setDefaultL(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory("android.intent.category.HOME");
try {
intent.setComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));
context.startActivity(intent);
} catch (Exception e) {
try {
intent.setComponent(new ComponentName("com.huawei.android.internal.app", "com.huawei.android.internal.app.HwResolverActivity"));//这个类有些华为手机找不到
context.startActivity(intent);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * 0.4f);
int height = Math.round(image.getHeight() * 0.4f);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
blurScript.setRadius(25f);
blurScript.setInput(tmpIn);
blurScript.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
public Bitmap getWallpaperBg(Context context) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();
Bitmap bitmap = cropImage(context, bm);
return bitmap;
}
private void SetWallPaper(Activity activity) {
//利用WallpaparManager,添加权限set_wallpaper
WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);
try {
wallpaperManager.setBitmap(BitmapFactory.decodeResource(activity.getResources(), R.drawable.wallpapers));
} catch (IOException e) {
e.printStackTrace();
Log.e("TT", e.getMessage());
}
SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("SetWallPaper", 1);
editor.apply();
//getSystemService(WALLPAPER_SERVICE)获取wallpaperManager会出问题
/* @SuppressLint("ServiceCast")
WallpaperManager manager =(WallpaperManager)getSystemService(WALLPAPER_SERVICE);
try {
manager.setBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.pic1));
} catch (IOException e) {
e.printStackTrace();
}*/
}
private Bitmap cropImage(Context context, Bitmap oldBitmap) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getRealMetrics(dm);
int width = dm.widthPixels; // 屏幕宽度像素1200
int height = dm.heightPixels; // 屏幕高度像素1920
int picWidth = oldBitmap.getWidth();
int picHeight = oldBitmap.getHeight();
float wScala = (float) height / width;
float bScala = (float) picHeight / picWidth;
int newPicWidth;
int newpicHeight;
Bitmap bitmap = null;
if (wScala > bScala) {
newPicWidth = (int) (picWidth / wScala);
newpicHeight = picHeight;
bitmap = Bitmap.createBitmap(oldBitmap, (newPicWidth - width) / 2, 0, newPicWidth, newpicHeight);
} else {
newPicWidth = picWidth;
newpicHeight = (int) (picHeight * wScala);
bitmap = Bitmap.createBitmap(oldBitmap, 0, (picHeight - newpicHeight) / 2, newPicWidth, newpicHeight);
}
return bitmap;
}
}

View File

@@ -0,0 +1,96 @@
package com.tt.ttutils.utils.java;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.List;
public class ProcessUtil {
private static String currentProcessName;
/**
* @return 当前进程名
*/
@Nullable
public static String getCurrentProcessName(@NonNull Context context) {
if (!TextUtils.isEmpty(currentProcessName)) {
return currentProcessName;
}
//1)通过Application的API获取当前进程名
currentProcessName = getCurrentProcessNameByApplication();
if (!TextUtils.isEmpty(currentProcessName)) {
return currentProcessName;
}
//2)通过反射ActivityThread获取当前进程名
currentProcessName = getCurrentProcessNameByActivityThread();
if (!TextUtils.isEmpty(currentProcessName)) {
return currentProcessName;
}
//3)通过ActivityManager获取当前进程名
currentProcessName = getCurrentProcessNameByActivityManager(context);
return currentProcessName;
}
/**
* 通过Application新的API获取进程名无需反射无需IPC效率最高。
*/
public static String getCurrentProcessNameByApplication() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return Application.getProcessName();
}
return null;
}
/**
* 通过反射ActivityThread获取进程名避免了ipc
*/
public static String getCurrentProcessNameByActivityThread() {
String processName = null;
try {
final Method declaredMethod = Class.forName("android.app.ActivityThread", false, Application.class.getClassLoader())
.getDeclaredMethod("currentProcessName", (Class<?>[]) new Class[0]);
declaredMethod.setAccessible(true);
final Object invoke = declaredMethod.invoke(null, new Object[0]);
if (invoke instanceof String) {
processName = (String) invoke;
}
} catch (Throwable e) {
e.printStackTrace();
}
return processName;
}
/**
* 通过ActivityManager 获取进程名需要IPC通信
*/
public static String getCurrentProcessNameByActivityManager(@NonNull Context context) {
if (context == null) {
return null;
}
int pid = android.os.Process.myPid();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (am != null) {
List<ActivityManager.RunningAppProcessInfo> runningAppList = am.getRunningAppProcesses();
if (runningAppList != null) {
for (ActivityManager.RunningAppProcessInfo processInfo : runningAppList) {
if (processInfo.pid == pid) {
return processInfo.processName;
}
}
}
}
return null;
}
}

View File

@@ -3,12 +3,17 @@ package com.tt.ttutils.utils.java;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import com.tt.ttutils.disklrucache.CacheHelper;
import java.util.Iterator;
import java.util.Set;
public class TTUtils {
private static final String TAG = TTUtils.class.getSimpleName();
@@ -60,5 +65,18 @@ public class TTUtils {
return false;
}
public static void getExtrax(Intent intent) {
if (intent == null) return;
String action = intent.getAction();
Log.e(TAG, "getExtrax: " + action);
Bundle bundle = intent.getExtras();
if (bundle == null) return;
Set<String> ks = bundle.keySet();
Iterator<String> iterator = ks.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Log.e("KEY", key);
Log.e("value", intent.getStringExtra(key));
}
}
}