1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.mockime; 18 19 import android.content.ComponentName; 20 import android.content.ContentProvider; 21 import android.content.ContentValues; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.util.Log; 26 import android.view.inputmethod.InputMethodManager; 27 import android.view.inputmethod.InputMethodSubtype; 28 29 import androidx.annotation.NonNull; 30 import androidx.annotation.Nullable; 31 32 /** 33 * {@link ContentProvider} to receive {@link ImeSettings} via 34 * {@link ContentProvider#call(String, String, String, Bundle)}. 35 */ 36 public class SettingsProvider extends ContentProvider { 37 38 public static final String SET_INLINE_SUGGESTION_EXTRAS_COMMAND = "setInlineSuggestionsExtras"; 39 private static final String TAG = "SettingsProvider"; 40 41 static final String SET_ADDITIONAL_SUBTYPES_COMMAND = "setAdditionalSubtypes"; 42 static final String SET_ADDITIONAL_SUBTYPES_KEY = "subtypes"; 43 44 @Nullable 45 private static ImeSettings sSettings = null; 46 @Nullable 47 private static Bundle sInlineSuggestionsExtras; 48 49 @Override onCreate()50 public boolean onCreate() { 51 return true; 52 } 53 54 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)55 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 56 String sortOrder) { 57 return null; 58 } 59 60 @Override getType(Uri uri)61 public String getType(Uri uri) { 62 return null; 63 } 64 65 @Override insert(Uri uri, ContentValues values)66 public Uri insert(Uri uri, ContentValues values) { 67 return null; 68 } 69 70 @Override delete(Uri uri, String selection, String[] selectionArgs)71 public int delete(Uri uri, String selection, String[] selectionArgs) { 72 return 0; 73 } 74 75 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)76 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 77 return 0; 78 } 79 80 @NonNull getMockImeId()81 private String getMockImeId() { 82 return new ComponentName(getContext().getPackageName(), MockIme.class.getName()) 83 .flattenToShortString(); 84 } 85 86 @Override call(String authority, String method, String arg, Bundle extras)87 public Bundle call(String authority, String method, String arg, Bundle extras) { 88 Log.i(TAG, String.format("SettingsProvider.call(): instance=%s, method=%s", this, method)); 89 if ("write".equals(method)) { 90 sSettings = null; 91 final String callingPackageName = getCallingPackage(); 92 if (callingPackageName == null) { 93 throw new SecurityException("Failed to obtain the calling package name."); 94 } 95 sSettings = new ImeSettings(callingPackageName, extras); 96 } else if (SET_ADDITIONAL_SUBTYPES_COMMAND.equals(method)) { 97 InputMethodSubtype[] additionalSubtypes = extras.getParcelableArray( 98 SET_ADDITIONAL_SUBTYPES_KEY, InputMethodSubtype.class); 99 if (additionalSubtypes == null) { 100 // IMM#setAdditionalInputMethodSubtypes() doesn't accept null array. 101 additionalSubtypes = new InputMethodSubtype[0]; 102 } 103 getContext().getSystemService(InputMethodManager.class) 104 .setAdditionalInputMethodSubtypes(getMockImeId(), additionalSubtypes); 105 } else if ("delete".equals(method)) { 106 if (sSettings != null) { 107 sSettings.close(); 108 } 109 sSettings = null; 110 sInlineSuggestionsExtras = null; 111 } else if (SET_INLINE_SUGGESTION_EXTRAS_COMMAND.equals(method)) { 112 sInlineSuggestionsExtras = extras; 113 } 114 return Bundle.EMPTY; 115 } 116 getSettings()117 static ImeSettings getSettings() { 118 return sSettings; 119 } 120 getInlineSuggestionsExtras()121 static Bundle getInlineSuggestionsExtras() { 122 return sInlineSuggestionsExtras; 123 } 124 } 125