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.ContentProvider; 20 import android.content.ContentValues; 21 import android.database.Cursor; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.view.inputmethod.InputMethodManager; 26 import android.view.inputmethod.InputMethodSubtype; 27 28 import androidx.annotation.Nullable; 29 30 /** 31 * {@link ContentProvider} to receive {@link ImeSettings} via 32 * {@link ContentProvider#call(String, String, String, Bundle)}. 33 */ 34 public class SettingsProvider extends ContentProvider { 35 36 private static final String TAG = "SettingsProvider"; 37 static final String AUTHORITY = "com.android.cts.mockime.provider"; 38 39 static final String SET_ADDITIONAL_SUBTYPES_COMMAND = "setAdditionalSubtypes"; 40 static final String SET_ADDITIONAL_SUBTYPES_KEY = "subtypes"; 41 42 @Nullable 43 private static ImeSettings sSettings = null; 44 45 @Override onCreate()46 public boolean onCreate() { 47 return true; 48 } 49 50 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)51 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 52 String sortOrder) { 53 return null; 54 } 55 56 @Override getType(Uri uri)57 public String getType(Uri uri) { 58 return null; 59 } 60 61 @Override insert(Uri uri, ContentValues values)62 public Uri insert(Uri uri, ContentValues values) { 63 return null; 64 } 65 66 @Override delete(Uri uri, String selection, String[] selectionArgs)67 public int delete(Uri uri, String selection, String[] selectionArgs) { 68 return 0; 69 } 70 71 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)72 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 73 return 0; 74 } 75 76 @Override call(String authority, String method, String arg, Bundle extras)77 public Bundle call(String authority, String method, String arg, Bundle extras) { 78 Log.i(TAG, String.format("SettingsProvider.call(): instance=%s, method=%s", this, method)); 79 if ("write".equals(method)) { 80 sSettings = null; 81 final String callingPackageName = getCallingPackage(); 82 if (callingPackageName == null) { 83 throw new SecurityException("Failed to obtain the calling package name."); 84 } 85 sSettings = new ImeSettings(callingPackageName, extras); 86 } else if (SET_ADDITIONAL_SUBTYPES_COMMAND.equals(method)) { 87 InputMethodSubtype[] additionalSubtypes = extras.getParcelableArray( 88 SET_ADDITIONAL_SUBTYPES_KEY, InputMethodSubtype.class); 89 if (additionalSubtypes == null) { 90 // IMM#setAdditionalInputMethodSubtypes() doesn't accept null array. 91 additionalSubtypes = new InputMethodSubtype[0]; 92 } 93 getContext().getSystemService(InputMethodManager.class) 94 .setAdditionalInputMethodSubtypes(MockIme.getImeId(), additionalSubtypes); 95 } else if ("delete".equals(method)) { 96 sSettings = null; 97 } 98 return Bundle.EMPTY; 99 } 100 getSettings()101 static ImeSettings getSettings() { 102 return sSettings; 103 } 104 } 105