1919import static android .view .ViewGroup .LayoutParams .MATCH_PARENT ;
2020import static android .view .ViewGroup .LayoutParams .WRAP_CONTENT ;
2121
22+ import android .app .ActivityManager ;
2223import android .app .Dialog ;
2324import android .content .Context ;
2425import android .content .res .Configuration ;
3839import android .util .Log ;
3940import android .util .PrintWriterPrinter ;
4041import android .util .Printer ;
42+ import android .view .Display ;
4143import android .view .KeyCharacterMap ;
4244import android .view .KeyEvent ;
4345import android .view .LayoutInflater ;
@@ -250,6 +252,7 @@ public class InputMethodService extends AbstractInputMethodService {
250252 InputMethodManager mImm ;
251253
252254 int mTheme = 0 ;
255+ boolean mHardwareAccelerated = false ;
253256
254257 LayoutInflater mInflater ;
255258 TypedArray mThemeAttrs ;
@@ -614,6 +617,26 @@ public void setTheme(int theme) {
614617 mTheme = theme ;
615618 }
616619
620+ /**
621+ * You can call this to try to enable hardware accelerated drawing for
622+ * your IME. This must be set before {@link #onCreate}, so you
623+ * will typically call it in your constructor. It is not always possible
624+ * to use hardware acclerated drawing in an IME (for example on low-end
625+ * devices that do not have the resources to support this), so the call
626+ * returns true if it succeeds otherwise false if you will need to draw
627+ * in software. You must be able to handle either case.
628+ */
629+ public boolean enableHardwareAcceleration () {
630+ if (mWindow != null ) {
631+ throw new IllegalStateException ("Must be called before onCreate()" );
632+ }
633+ if (ActivityManager .isHighEndGfx (new Display (Display .DEFAULT_DISPLAY , null ))) {
634+ mHardwareAccelerated = true ;
635+ return true ;
636+ }
637+ return false ;
638+ }
639+
617640 @ Override public void onCreate () {
618641 mTheme = Resources .selectSystemTheme (mTheme ,
619642 getApplicationInfo ().targetSdkVersion ,
@@ -626,6 +649,9 @@ public void setTheme(int theme) {
626649 mInflater = (LayoutInflater )getSystemService (
627650 Context .LAYOUT_INFLATER_SERVICE );
628651 mWindow = new SoftInputWindow (this , mTheme , mDispatcherState );
652+ if (mHardwareAccelerated ) {
653+ mWindow .getWindow ().addFlags (WindowManager .LayoutParams .FLAG_HARDWARE_ACCELERATED );
654+ }
629655 initViews ();
630656 mWindow .getWindow ().setLayout (MATCH_PARENT , WRAP_CONTENT );
631657 }
0 commit comments