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