mirror of
https://github.com/Tencent/Hippy.git
synced 2026-10-25 18:50:35 +08:00
refactor(android): can not access clip data on background
This commit is contained in:
@@ -93,7 +93,7 @@ public abstract class HippyEngine {
|
||||
}
|
||||
params.check();
|
||||
LogUtils.enableDebugLog(params.enableLog);
|
||||
ContextHolder.setContext(params.context);
|
||||
ContextHolder.initAppContext(params.context);
|
||||
|
||||
HippyEngine hippyEngine;
|
||||
if (params.groupId == -1) {
|
||||
|
||||
@@ -33,7 +33,7 @@ public abstract class HippyEngineHost {
|
||||
|
||||
public HippyEngineHost(Application application) {
|
||||
mApplication = application;
|
||||
ContextHolder.setContext(mApplication);
|
||||
ContextHolder.initAppContext(mApplication);
|
||||
}
|
||||
|
||||
public HippyEngineManager createDebugHippyEngineManager(String debugJs) {
|
||||
|
||||
+45
-34
@@ -9,45 +9,56 @@ import com.tencent.mtt.hippy.annotation.HippyNativeModule;
|
||||
import com.tencent.mtt.hippy.modules.Promise;
|
||||
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
|
||||
import android.content.ClipboardManager;
|
||||
import com.tencent.mtt.hippy.utils.ContextHolder;
|
||||
|
||||
@HippyNativeModule(name = "ClipboardModule")
|
||||
public class ClipboardModule extends HippyNativeModuleBase {
|
||||
|
||||
private final ClipboardManager mClipboardManager;
|
||||
private final ClipboardManager mClipboardManager;
|
||||
|
||||
public ClipboardModule(HippyEngineContext context) {
|
||||
super(context);
|
||||
mClipboardManager = (ClipboardManager) (mContext.getGlobalConfigs()).getContext()
|
||||
.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
}
|
||||
|
||||
|
||||
private ClipboardManager getClipboardService() {
|
||||
return mClipboardManager;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@HippyMethod(name = "getString")
|
||||
public void getString(Promise promise) {
|
||||
try {
|
||||
ClipboardManager clipboard = getClipboardService();
|
||||
ClipData clipData = clipboard.getPrimaryClip();
|
||||
if (clipData != null && clipData.getItemCount() >= 1) {
|
||||
ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
|
||||
promise.resolve("" + firstItem.getText());
|
||||
} else {
|
||||
promise.resolve("");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
promise.reject(e);
|
||||
public ClipboardModule(HippyEngineContext context) {
|
||||
super(context);
|
||||
mClipboardManager = (ClipboardManager) (mContext.getGlobalConfigs()).getContext()
|
||||
.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@HippyMethod(name = "setString")
|
||||
public void setString(String text) {
|
||||
ClipData clipdata = ClipData.newPlainText(null, text);
|
||||
ClipboardManager clipboard = getClipboardService();
|
||||
clipboard.setPrimaryClip(clipdata);
|
||||
}
|
||||
private ClipboardManager getClipboardService() {
|
||||
return mClipboardManager;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@HippyMethod(name = "getString")
|
||||
public void getString(Promise promise) {
|
||||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
|
||||
if (ContextHolder.isAppOnBackground()) {
|
||||
promise.resolve("");
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
ClipboardManager clipboard = getClipboardService();
|
||||
ClipData clipData = clipboard.getPrimaryClip();
|
||||
if (clipData != null && clipData.getItemCount() >= 1) {
|
||||
ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
|
||||
promise.resolve("" + firstItem.getText());
|
||||
} else {
|
||||
promise.resolve("");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@HippyMethod(name = "setString")
|
||||
public void setString(String text) {
|
||||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
|
||||
if (ContextHolder.isAppOnBackground()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
ClipData clipdata = ClipData.newPlainText(null, text);
|
||||
ClipboardManager clipboard = getClipboardService();
|
||||
clipboard.setPrimaryClip(clipdata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,19 +15,44 @@
|
||||
*/
|
||||
package com.tencent.mtt.hippy.utils;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.ActivityManager.RunningAppProcessInfo;
|
||||
import android.content.Context;
|
||||
import java.util.List;
|
||||
|
||||
public class ContextHolder {
|
||||
|
||||
private static Context applicationContext = null;
|
||||
private static Context sAppContext;
|
||||
|
||||
public static void setContext(Context context) {
|
||||
if (context != null && applicationContext == null) {
|
||||
applicationContext = context.getApplicationContext();
|
||||
public static void initAppContext(Context context) {
|
||||
if (context != null && sAppContext == null) {
|
||||
sAppContext = context.getApplicationContext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Context getAppContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
public static Context getAppContext() {
|
||||
return sAppContext;
|
||||
}
|
||||
|
||||
public static boolean isAppOnBackground() {
|
||||
if (sAppContext == null) {
|
||||
return true;
|
||||
}
|
||||
ActivityManager activityManager = (ActivityManager) sAppContext
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
|
||||
if (appProcesses == null) {
|
||||
return true;
|
||||
}
|
||||
int myPid = android.os.Process.myPid();
|
||||
for (RunningAppProcessInfo appProcess : appProcesses) {
|
||||
if (appProcess.pid == myPid) {
|
||||
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user