android: kotlinify ui helper (#21879)
This commit is contained in:
parent
2a95f7cfac
commit
5ff1d70357
|
@ -1,124 +0,0 @@
|
||||||
package im.status.ethereum.module;
|
|
||||||
|
|
||||||
import com.facebook.react.bridge.ReactMethod;
|
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.webkit.WebView;
|
|
||||||
import android.webkit.CookieManager;
|
|
||||||
import android.webkit.CookieSyncManager;
|
|
||||||
import android.webkit.WebStorage;
|
|
||||||
import android.view.WindowManager;
|
|
||||||
import com.facebook.react.uimanager.UIManagerModule;
|
|
||||||
import com.facebook.react.uimanager.UIBlock;
|
|
||||||
import com.facebook.react.uimanager.NativeViewHierarchyManager;
|
|
||||||
import android.view.inputmethod.InputMethodManager;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.EditText;
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
public class UIHelper extends ReactContextBaseJavaModule {
|
|
||||||
private static final String TAG = "UIHelper";
|
|
||||||
private ReactApplicationContext reactContext;
|
|
||||||
|
|
||||||
public UIHelper(ReactApplicationContext reactContext) {
|
|
||||||
super(reactContext);
|
|
||||||
this.reactContext = reactContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "UIHelper";
|
|
||||||
}
|
|
||||||
|
|
||||||
@ReactMethod
|
|
||||||
public void setSoftInputMode(final int mode) {
|
|
||||||
Log.d(TAG, "setSoftInputMode");
|
|
||||||
final Activity activity = getCurrentActivity();
|
|
||||||
if (activity == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
activity.getWindow().setSoftInputMode(mode);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@ReactMethod
|
|
||||||
public void clearCookies() {
|
|
||||||
Log.d(TAG, "clearCookies");
|
|
||||||
final Activity activity = getCurrentActivity();
|
|
||||||
if (activity == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
|
|
||||||
CookieManager.getInstance().removeAllCookies(null);
|
|
||||||
CookieManager.getInstance().flush();
|
|
||||||
} else {
|
|
||||||
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(activity);
|
|
||||||
cookieSyncManager.startSync();
|
|
||||||
CookieManager cookieManager = CookieManager.getInstance();
|
|
||||||
cookieManager.removeAllCookie();
|
|
||||||
cookieManager.removeSessionCookie();
|
|
||||||
cookieSyncManager.stopSync();
|
|
||||||
cookieSyncManager.sync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ReactMethod
|
|
||||||
public void toggleWebviewDebug(final boolean val) {
|
|
||||||
Log.d(TAG, "toggleWebviewDebug");
|
|
||||||
final Activity activity = getCurrentActivity();
|
|
||||||
if (activity == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
WebView.setWebContentsDebuggingEnabled(val);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@ReactMethod
|
|
||||||
public void clearStorageAPIs() {
|
|
||||||
Log.d(TAG, "clearStorageAPIs");
|
|
||||||
final Activity activity = getCurrentActivity();
|
|
||||||
if (activity == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
WebStorage storage = WebStorage.getInstance();
|
|
||||||
if (storage != null) {
|
|
||||||
storage.deleteAllData();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ReactMethod
|
|
||||||
public void resetKeyboardInputCursor(final int reactTagToReset, final int selection) {
|
|
||||||
UIManagerModule uiManager = getReactApplicationContext().getNativeModule(UIManagerModule.class);
|
|
||||||
uiManager.addUIBlock(new UIBlock() {
|
|
||||||
@Override
|
|
||||||
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
|
|
||||||
InputMethodManager imm = (InputMethodManager) getReactApplicationContext().getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
||||||
if (imm != null) {
|
|
||||||
View viewToReset = nativeViewHierarchyManager.resolveView(reactTagToReset);
|
|
||||||
imm.restartInput(viewToReset);
|
|
||||||
try {
|
|
||||||
EditText textView = (EditText) viewToReset;
|
|
||||||
textView.setSelection(selection);
|
|
||||||
} catch (Exception e) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
package im.status.ethereum.module
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.View
|
||||||
|
import android.view.WindowManager
|
||||||
|
import android.view.inputmethod.InputMethodManager
|
||||||
|
import android.webkit.CookieManager
|
||||||
|
import android.webkit.CookieSyncManager
|
||||||
|
import android.webkit.WebStorage
|
||||||
|
import android.webkit.WebView
|
||||||
|
import android.widget.EditText
|
||||||
|
import com.facebook.react.bridge.ReactApplicationContext
|
||||||
|
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
||||||
|
import com.facebook.react.bridge.ReactMethod
|
||||||
|
import com.facebook.react.uimanager.NativeViewHierarchyManager
|
||||||
|
import com.facebook.react.uimanager.UIBlock
|
||||||
|
import com.facebook.react.uimanager.UIManagerModule
|
||||||
|
|
||||||
|
class UIHelper(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "UIHelper"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getName(): String = "UIHelper"
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
fun setSoftInputMode(mode: Int) {
|
||||||
|
Log.d(TAG, "setSoftInputMode")
|
||||||
|
getCurrentActivity()?.run {
|
||||||
|
runOnUiThread {
|
||||||
|
window.setSoftInputMode(mode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
@ReactMethod
|
||||||
|
fun clearCookies() {
|
||||||
|
Log.d(TAG, "clearCookies")
|
||||||
|
getCurrentActivity()?.let { activity ->
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
|
CookieManager.getInstance().apply {
|
||||||
|
removeAllCookies(null)
|
||||||
|
flush()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
CookieSyncManager.createInstance(activity).also { cookieSyncManager ->
|
||||||
|
cookieSyncManager.startSync()
|
||||||
|
CookieManager.getInstance().apply {
|
||||||
|
removeAllCookie()
|
||||||
|
removeSessionCookie()
|
||||||
|
}
|
||||||
|
cookieSyncManager.stopSync()
|
||||||
|
cookieSyncManager.sync()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
fun toggleWebviewDebug(enabled: Boolean) {
|
||||||
|
Log.d(TAG, "toggleWebviewDebug")
|
||||||
|
getCurrentActivity()?.run {
|
||||||
|
runOnUiThread {
|
||||||
|
WebView.setWebContentsDebuggingEnabled(enabled)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
fun clearStorageAPIs() {
|
||||||
|
Log.d(TAG, "clearStorageAPIs")
|
||||||
|
getCurrentActivity()?.let {
|
||||||
|
WebStorage.getInstance()?.deleteAllData()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
fun resetKeyboardInputCursor(reactTagToReset: Int, selection: Int) {
|
||||||
|
reactApplicationContext.getNativeModule(UIManagerModule::class.java)?.addUIBlock(object : UIBlock {
|
||||||
|
override fun execute(nativeViewHierarchyManager: NativeViewHierarchyManager) {
|
||||||
|
(reactApplicationContext.baseContext.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)?.let { imm ->
|
||||||
|
val viewToReset = nativeViewHierarchyManager.resolveView(reactTagToReset)
|
||||||
|
imm.restartInput(viewToReset)
|
||||||
|
try {
|
||||||
|
(viewToReset as? EditText)?.setSelection(selection)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// Ignore exceptions during selection setting
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue