1 /*
2  * Copyright (C) 2012 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.android.mail.ui;
18 
19 import android.app.ActionBar;
20 import android.app.Activity;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.Selection;
25 import android.text.TextUtils;
26 import android.view.KeyEvent;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.view.inputmethod.EditorInfo;
30 import android.widget.EditText;
31 import android.widget.TextView;
32 
33 import com.android.mail.R;
34 
35 /**
36  * This activity prompts the user for a name for the shortcut to their folder
37  * This activity could, in the future, be used to allow the user to specify an
38  * icon for the shortcut
39  */
40 public class ShortcutNameActivity extends Activity implements OnClickListener,
41         TextView.OnEditorActionListener {
42     /*package*/static final String EXTRA_FOLDER_CLICK_INTENT = "extra_folder_click_intent";
43 
44     /*package*/static final String EXTRA_SHORTCUT_NAME = "extra_shortcut_name";
45 
46     private EditText mFolderText;
47     private String mFolderName;
48 
49     private Intent mShortcutClickIntent;
50 
51     @Override
onCreate(Bundle icicle)52     public void onCreate(Bundle icicle) {
53         super.onCreate(icicle);
54         setContentView(R.layout.shortcut_name_activity);
55 
56         mShortcutClickIntent = (Intent)getIntent().getParcelableExtra(EXTRA_FOLDER_CLICK_INTENT);
57         mFolderName = getIntent().getStringExtra(EXTRA_SHORTCUT_NAME);
58 
59         mFolderText = (EditText) findViewById(R.id.folder_text);
60         mFolderText.setText(mFolderName);
61         mFolderText.setOnEditorActionListener(this);
62 
63         // Set focus to end of input line
64         mFolderText.requestFocus();
65         Editable editableText = mFolderText.getText();
66         Selection.setSelection(editableText, editableText.length());
67 
68         findViewById(R.id.done).setOnClickListener(this);
69         findViewById(R.id.cancel).setOnClickListener(this);
70         ActionBar actionBar = getActionBar();
71         if (actionBar != null) {
72             actionBar.setIcon(R.mipmap.ic_launcher_shortcut_folder);
73         }
74     }
75 
76     @Override
onClick(View v)77     public void onClick(View v) {
78         final int id = v.getId();
79         if (R.id.done == id) {
80             doCreateShortcut();
81         } else if (R.id.cancel == id) {
82             doCancel();
83         }
84     }
85 
86     @Override
onEditorAction(TextView v, int actionId, KeyEvent event)87     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
88         if (actionId == EditorInfo.IME_ACTION_DONE) {
89             doCreateShortcut();
90             return true;
91         }
92         return false;
93     }
94 
doCreateShortcut()95     private void doCreateShortcut() {
96         // Get the name that the user entered
97         CharSequence userShortcutName = mFolderText.getText();
98 
99         Intent resultIntent = new Intent();
100         resultIntent.putExtra(EXTRA_FOLDER_CLICK_INTENT, mShortcutClickIntent);
101         // Initially set the name of the shortcut to the name of the folder.
102         // If the user sets a valid name, the user specified one will be used.
103         resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mFolderName);
104 
105         final String shortcutName = userShortcutName.toString();
106         if (TextUtils.getTrimmedLength(shortcutName) > 0) {
107             mShortcutClickIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
108         }
109 
110         setResult(RESULT_OK, mShortcutClickIntent);
111         finish();
112     }
113 
doCancel()114     private void doCancel() {
115         setResult(RESULT_CANCELED);
116         finish();
117     }
118 
119 }
120