Skip to content

Commit 6025b9a

Browse files
Ming-Shin Luandroid-build-merge-worker-robot
authored andcommitted
EditTextVariations: create a IME focusable overlay for test am: f134ea3
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/inputmethods/LatinIME/+/17759925 Change-Id: I20f87064cd0f846bff7214601233a4d2d6bf8f32 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2 parents ab95987 + f134ea3 commit 6025b9a

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

tools/EditTextVariations/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
android:versionName="0.67"
2020
android:versionCode="67">
2121
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
22+
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2223
<supports-screens android:resizeable="true"/>
2324
<uses-sdk android:targetSdkVersion="27"
2425
android:minSdkVersion="11"/>

tools/EditTextVariations/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
<string name="menu_softinput_hidden" translatable="false">Keyboard Hidden</string>
3636
<!-- The menu title to send a notification to test direct reply. [CHAR LIMIT=20] -->
3737
<string name="menu_direct_reply">Direct Reply</string>
38+
<!-- The menu title to show a application overlay with NOT_FOCUSABLE | ALT_FOCUSABLE_IM. [CHAR LIMIT=26] -->
39+
<string name="menu_show_ime_focusable_overlay">Show IME focusable overlay</string>
40+
<!-- The menu title to hide a application overlay with NOT_FOCUSABLE | ALT_FOCUSABLE_IM. [CHAR LIMIT=26] -->
41+
<string name="menu_hide_ime_focusable_overlay">Hide IME focusable overlay</string>
3842
<!-- The example of custom action key label. Must be short to fit on key. 5 chars or less is preferable. [CHAR LIMIT=7] -->
3943
<string name="custom_action_label">Custom</string>
4044
</resources>

tools/EditTextVariations/src/com/android/inputmethod/tools/edittextvariations/EditTextVariations.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,31 @@
1616

1717
package com.android.inputmethod.tools.edittextvariations;
1818

19+
import static android.graphics.Color.BLUE;
20+
import static android.view.Gravity.LEFT;
21+
import static android.view.Gravity.TOP;
22+
import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
23+
import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
24+
import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
25+
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
26+
1927
import android.Manifest;
2028
import android.annotation.SuppressLint;
2129
import android.app.Activity;
2230
import android.app.AlertDialog;
31+
import android.content.Context;
2332
import android.content.DialogInterface;
2433
import android.content.Intent;
2534
import android.content.SharedPreferences;
2635
import android.content.pm.ApplicationInfo;
2736
import android.content.pm.PackageInfo;
2837
import android.content.pm.PackageManager;
2938
import android.content.pm.PackageManager.NameNotFoundException;
39+
import android.graphics.Rect;
3040
import android.os.Build;
3141
import android.os.Bundle;
3242
import android.preference.PreferenceManager;
43+
import android.provider.Settings;
3344
import android.text.InputType;
3445
import android.text.TextUtils;
3546
import android.util.Log;
@@ -65,6 +76,7 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi
6576
private static final int MENU_SOFTINPUT_VISIBLE = 4;
6677
private static final int MENU_SOFTINPUT_HIDDEN = 5;
6778
private static final int MENU_DIRECT_REPLY = 6;
79+
private static final int MENU_TOGGLE_IME_FOCUSABLE_OVERLAY = 7;
6880
private static final String PREF_THEME = "theme";
6981
private static final String PREF_NAVIGATE = "navigate";
7082
private static final String PREF_SOFTINPUT = "softinput";
@@ -85,6 +97,9 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi
8597

8698
private ArrayAdapter<String> mAutoCompleteAdapter;
8799

100+
private TextView mOverlayTextView;
101+
private boolean mShowOverlay = true;
102+
88103
/** Called when the activity is first created. */
89104
@SuppressLint("SetJavaScriptEnabled")
90105
@Override
@@ -171,9 +186,12 @@ public boolean onCreateOptionsMenu(final Menu menu) {
171186
if (NotificationUtils.DIRECT_REPLY_SUPPORTED) {
172187
menu.add(Menu.NONE, MENU_DIRECT_REPLY, 5, R.string.menu_direct_reply);
173188
}
189+
menu.add(Menu.NONE, MENU_TOGGLE_IME_FOCUSABLE_OVERLAY, 6,
190+
mShowOverlay ? getString(R.string.menu_show_ime_focusable_overlay)
191+
: getString(R.string.menu_hide_ime_focusable_overlay));
174192
try {
175193
final PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
176-
menu.add(Menu.NONE, MENU_VERSION, 6,
194+
menu.add(Menu.NONE, MENU_VERSION, 7,
177195
getString(R.string.menu_version, pinfo.versionName))
178196
.setEnabled(false);
179197
} catch (NameNotFoundException e) {
@@ -213,6 +231,16 @@ && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) !=
213231
} else {
214232
NotificationUtils.sendDirectReplyNotification(this);
215233
}
234+
} else if (itemId == MENU_TOGGLE_IME_FOCUSABLE_OVERLAY) {
235+
if (!Settings.canDrawOverlays(this)) {
236+
Toast.makeText(this,
237+
"Not allowed to show overlay.\nCheck \"Settings > "
238+
+ "Display over other apps\"", Toast.LENGTH_LONG).show();
239+
} else {
240+
toggleOverlayView(true /* needsIme */);
241+
item.setTitle(mShowOverlay ? getString(R.string.menu_show_ime_focusable_overlay)
242+
: getString(R.string.menu_hide_ime_focusable_overlay));
243+
}
216244
}
217245
return true;
218246
}
@@ -233,6 +261,14 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions,
233261
}
234262
}
235263

264+
@Override
265+
protected void onDestroy() {
266+
if (mOverlayTextView != null) {
267+
getWindowManager().removeView(mOverlayTextView);
268+
mOverlayTextView = null;
269+
}
270+
}
271+
236272
@Override
237273
public void onClick(final DialogInterface dialog, final int which) {
238274
saveTheme(ThemeItem.THEME_LIST.get(which));
@@ -515,4 +551,26 @@ private static boolean isNeedNotificationPermission() {
515551
}
516552
return false;
517553
}
554+
555+
private void toggleOverlayView(boolean needsIme) {
556+
if (mOverlayTextView == null) {
557+
Context overlayContext = createDisplayContext(getDisplay())
558+
.createWindowContext(TYPE_APPLICATION_OVERLAY, null /* options */);
559+
int focusableFlags = FLAG_NOT_FOCUSABLE | (needsIme ? FLAG_ALT_FOCUSABLE_IM : 0);
560+
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
561+
TYPE_APPLICATION_OVERLAY, FLAG_WATCH_OUTSIDE_TOUCH | focusableFlags);
562+
final Rect windowBounds = getWindowManager().getCurrentWindowMetrics().getBounds();
563+
params.width = windowBounds.width() / 3;
564+
params.height = windowBounds.height() / 3;
565+
params.gravity = TOP | LEFT;
566+
567+
mOverlayTextView = new TextView(overlayContext);
568+
mOverlayTextView.setText("I'm an IME focusable overlay");
569+
mOverlayTextView.setBackgroundColor(BLUE);
570+
getWindowManager().addView(mOverlayTextView, params);
571+
}
572+
mOverlayTextView.setVisibility(mShowOverlay ? View.VISIBLE : View.GONE);
573+
// Toggle the overlay visibility after the call.
574+
mShowOverlay = !mShowOverlay;
575+
}
518576
}

0 commit comments

Comments
 (0)