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 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 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 final Bundle args = intent.getExtras(); 60 args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, mode); 61 62 if (null != savedInstanceState) { 63 // Override options if we have a saved state. 64 args.putAll(savedInstanceState); 65 } 66 67 mContents = new UserDictionaryAddWordContents(getWindow().getDecorView(), args); 68 } 69 70 @Override 71 public void onSaveInstanceState(final Bundle outState) { 72 mContents.saveStateIntoBundle(outState); 73 } 74 75 private void reportBackToCaller(final int resultCode, final Bundle result) { 76 final Intent senderIntent = getIntent(); 77 final Object listener = senderIntent.getExtras().get("listener"); 78 if (!(listener instanceof Messenger)) return; // This will work if listener is null too. 79 final Messenger messenger = (Messenger)listener; 80 81 final Message m = Message.obtain(); 82 m.obj = result; 83 m.what = resultCode; 84 try { 85 messenger.send(m); 86 } catch (RemoteException e) { 87 // Couldn't report back, but there is nothing we can do to fix it 88 } 89 } 90 91 public void onClickCancel(final View v) { 92 reportBackToCaller(CODE_CANCEL, null); 93 finish(); 94 } 95 96 public void onClickConfirm(final View v) { 97 final Bundle parameters = new Bundle(); 98 final int resultCode = mContents.apply(this, parameters); 99 reportBackToCaller(resultCode, parameters); 100 finish(); 101 } 102} 103