1/* 2 * Copyright (C) 2011 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 17package com.android.settings.inputmethod; 18 19import com.android.settings.R; 20 21import android.app.Activity; 22import android.content.Intent; 23import android.os.Bundle; 24import android.os.Message; 25import android.os.Messenger; 26import android.os.RemoteException; 27import android.view.View; 28 29public class UserDictionaryAddWordActivity extends Activity { 30 31 private static final String STATE_KEY_IS_OPEN = "isOpen"; 32 33 public static final String MODE_EDIT_ACTION = "com.android.settings.USER_DICTIONARY_EDIT"; 34 public static final String MODE_INSERT_ACTION = "com.android.settings.USER_DICTIONARY_INSERT"; 35 36 /* package */ static final int CODE_WORD_ADDED = 0; 37 /* package */ static final int CODE_CANCEL = 1; 38 /* package */ static final int CODE_ALREADY_PRESENT = 2; 39 40 private UserDictionaryAddWordContents mContents; 41 42 @Override 43 public void onCreate(final Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.user_dictionary_add_word); 46 final Intent intent = getIntent(); 47 final String action = intent.getAction(); 48 final int mode; 49 if (MODE_EDIT_ACTION.equals(action)) { 50 mode = UserDictionaryAddWordContents.MODE_EDIT; 51 } else if (MODE_INSERT_ACTION.equals(action)) { 52 mode = UserDictionaryAddWordContents.MODE_INSERT; 53 } else { 54 // Can never come here because we only support these two actions in the manifest 55 throw new RuntimeException("Unsupported action: " + action); 56 } 57 58 // The following will get the EXTRA_WORD and EXTRA_LOCALE fields that are in the intent. 59 // We do need to add the action by hand, because UserDictionaryAddWordContents expects 60 // it to be in the bundle, in the EXTRA_MODE key. 61 final Bundle args = intent.getExtras(); 62 args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, mode); 63 64 if (null != savedInstanceState) { 65 // Override options if we have a saved state. 66 args.putAll(savedInstanceState); 67 } 68 69 mContents = new UserDictionaryAddWordContents(getWindow().getDecorView(), args); 70 } 71 72 @Override 73 public void onSaveInstanceState(final Bundle outState) { 74 mContents.saveStateIntoBundle(outState); 75 } 76 77 private void reportBackToCaller(final int resultCode, final Bundle result) { 78 final Intent senderIntent = getIntent(); 79 final Object listener = senderIntent.getExtras().get("listener"); 80 if (!(listener instanceof Messenger)) return; // This will work if listener is null too. 81 final Messenger messenger = (Messenger)listener; 82 83 final Message m = Message.obtain(); 84 m.obj = result; 85 m.what = resultCode; 86 try { 87 messenger.send(m); 88 } catch (RemoteException e) { 89 // Couldn't report back, but there is nothing we can do to fix it 90 } 91 } 92 93 public void onClickCancel(final View v) { 94 reportBackToCaller(CODE_CANCEL, null); 95 finish(); 96 } 97 98 public void onClickConfirm(final View v) { 99 final Bundle parameters = new Bundle(); 100 final int resultCode = mContents.apply(this, parameters); 101 reportBackToCaller(resultCode, parameters); 102 finish(); 103 } 104} 105