diff options
Diffstat (limited to 'tools/EditTextVariations/src/com/android/inputmethod/tools/edittextvariations/EditTextVariations.java')
-rw-r--r-- | tools/EditTextVariations/src/com/android/inputmethod/tools/edittextvariations/EditTextVariations.java | 101 |
1 files changed, 99 insertions, 2 deletions
diff --git a/tools/EditTextVariations/src/com/android/inputmethod/tools/edittextvariations/EditTextVariations.java b/tools/EditTextVariations/src/com/android/inputmethod/tools/edittextvariations/EditTextVariations.java index 6eb85a528..53d08b6c9 100644 --- a/tools/EditTextVariations/src/com/android/inputmethod/tools/edittextvariations/EditTextVariations.java +++ b/tools/EditTextVariations/src/com/android/inputmethod/tools/edittextvariations/EditTextVariations.java @@ -16,18 +16,31 @@ package com.android.inputmethod.tools.edittextvariations; +import static android.graphics.Color.BLUE; +import static android.view.Gravity.LEFT; +import static android.view.Gravity.TOP; +import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; +import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; +import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; +import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; + +import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; +import android.graphics.Rect; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; +import android.provider.Settings; import android.text.InputType; import android.text.TextUtils; import android.util.Log; @@ -45,7 +58,9 @@ import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.TextView; +import android.widget.Toast; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -61,9 +76,11 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi private static final int MENU_SOFTINPUT_VISIBLE = 4; private static final int MENU_SOFTINPUT_HIDDEN = 5; private static final int MENU_DIRECT_REPLY = 6; + private static final int MENU_TOGGLE_IME_FOCUSABLE_OVERLAY = 7; private static final String PREF_THEME = "theme"; private static final String PREF_NAVIGATE = "navigate"; private static final String PREF_SOFTINPUT = "softinput"; + private static final int NOTIFICATION_PERMISSION_REQUEST_CODE = 0; private SharedPreferences prefs; private View[] fields; @@ -80,6 +97,9 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi private ArrayAdapter<String> mAutoCompleteAdapter; + private TextView mOverlayTextView; + private boolean mShowOverlay = true; + /** Called when the activity is first created. */ @SuppressLint("SetJavaScriptEnabled") @Override @@ -166,9 +186,12 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi if (NotificationUtils.DIRECT_REPLY_SUPPORTED) { menu.add(Menu.NONE, MENU_DIRECT_REPLY, 5, R.string.menu_direct_reply); } + menu.add(Menu.NONE, MENU_TOGGLE_IME_FOCUSABLE_OVERLAY, 6, + mShowOverlay ? getString(R.string.menu_show_ime_focusable_overlay) + : getString(R.string.menu_hide_ime_focusable_overlay)); try { final PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0); - menu.add(Menu.NONE, MENU_VERSION, 6, + menu.add(Menu.NONE, MENU_VERSION, 7, getString(R.string.menu_version, pinfo.versionName)) .setEnabled(false); } catch (NameNotFoundException e) { @@ -199,12 +222,54 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi saveSoftInputMode(itemId == MENU_SOFTINPUT_VISIBLE); restartActivity(); } else if (itemId == MENU_DIRECT_REPLY) { - NotificationUtils.sendDirectReplyNotification(this); + final boolean needPermissionCheck = isNeedNotificationPermission() + && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != + PackageManager.PERMISSION_GRANTED; + if (needPermissionCheck) { + requestPermissions(new String[] { Manifest.permission.POST_NOTIFICATIONS }, + NOTIFICATION_PERMISSION_REQUEST_CODE); + } else { + NotificationUtils.sendDirectReplyNotification(this); + } + } else if (itemId == MENU_TOGGLE_IME_FOCUSABLE_OVERLAY) { + if (!Settings.canDrawOverlays(this)) { + Toast.makeText(this, + "Not allowed to show overlay.\nCheck \"Settings > " + + "Display over other apps\"", Toast.LENGTH_LONG).show(); + } else { + toggleOverlayView(true /* needsIme */); + item.setTitle(mShowOverlay ? getString(R.string.menu_show_ime_focusable_overlay) + : getString(R.string.menu_hide_ime_focusable_overlay)); + } } return true; } @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, + int[] grantResults) { + if (requestCode == NOTIFICATION_PERMISSION_REQUEST_CODE) { + if (grantResults.length == 1 && + grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // Permission is granted. Continue to send the notification. + NotificationUtils.sendDirectReplyNotification(this); + } else { + Log.d(TAG, "POST_NOTIFICATIONS Permissions denied."); + Toast.makeText(this, "Required permission has denied", + Toast.LENGTH_LONG).show(); + } + } + } + + @Override + protected void onDestroy() { + if (mOverlayTextView != null) { + getWindowManager().removeView(mOverlayTextView); + mOverlayTextView = null; + } + } + + @Override public void onClick(final DialogInterface dialog, final int which) { saveTheme(ThemeItem.THEME_LIST.get(which)); restartActivity(); @@ -476,4 +541,36 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi } return text; } + + private static boolean isNeedNotificationPermission() { + for(Field field : Manifest.permission.class.getFields()) { + if (field.getName().equals("POST_NOTIFICATIONS")) { + Log.d(TAG, "Need notification permission."); + return true; + } + } + return false; + } + + private void toggleOverlayView(boolean needsIme) { + if (mOverlayTextView == null) { + Context overlayContext = createDisplayContext(getDisplay()) + .createWindowContext(TYPE_APPLICATION_OVERLAY, null /* options */); + int focusableFlags = FLAG_NOT_FOCUSABLE | (needsIme ? FLAG_ALT_FOCUSABLE_IM : 0); + final WindowManager.LayoutParams params = new WindowManager.LayoutParams( + TYPE_APPLICATION_OVERLAY, FLAG_WATCH_OUTSIDE_TOUCH | focusableFlags); + final Rect windowBounds = getWindowManager().getCurrentWindowMetrics().getBounds(); + params.width = windowBounds.width() / 3; + params.height = windowBounds.height() / 3; + params.gravity = TOP | LEFT; + + mOverlayTextView = new TextView(overlayContext); + mOverlayTextView.setText("I'm an IME focusable overlay"); + mOverlayTextView.setBackgroundColor(BLUE); + getWindowManager().addView(mOverlayTextView, params); + } + mOverlayTextView.setVisibility(mShowOverlay ? View.VISIBLE : View.GONE); + // Toggle the overlay visibility after the call. + mShowOverlay = !mShowOverlay; + } } |