1 /*
2  * Copyright (C) 2012 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.calendar.alerts;
18 
19 import android.app.ListActivity;
20 import android.content.ActivityNotFoundException;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.AdapterView;
25 import android.widget.AdapterView.OnItemClickListener;
26 import android.widget.ArrayAdapter;
27 import android.widget.Toast;
28 
29 import com.android.calendar.R;
30 import com.android.calendar.Utils;
31 
32 import java.util.Arrays;
33 
34 /**
35  * Activity which displays when the user wants to email guests from notifications.
36  *
37  * This presents the user with list if quick responses to be populated in an email
38  * to minimize typing.
39  *
40  */
41 public class QuickResponseActivity extends ListActivity implements OnItemClickListener {
42     private static final String TAG = "QuickResponseActivity";
43     public static final String EXTRA_EVENT_ID = "eventId";
44 
45     private String[] mResponses = null;
46     static long mEventId;
47 
48     @Override
onCreate(Bundle icicle)49     protected void onCreate(Bundle icicle) {
50         super.onCreate(icicle);
51 
52         Intent intent = getIntent();
53         if (intent == null) {
54             finish();
55             return;
56         }
57 
58         mEventId = intent.getLongExtra(EXTRA_EVENT_ID, -1);
59         if (mEventId == -1) {
60             finish();
61             return;
62         }
63 
64         // Set listener
65         getListView().setOnItemClickListener(QuickResponseActivity.this);
66 
67         // Populate responses
68         String[] responses = Utils.getQuickResponses(this);
69         Arrays.sort(responses);
70 
71         // Add "Custom response..."
72         mResponses = new String[responses.length + 1];
73         int i;
74         for (i = 0; i < responses.length; i++) {
75             mResponses[i] = responses[i];
76         }
77         mResponses[i] = getResources().getString(R.string.quick_response_custom_msg);
78 
79         setListAdapter(new ArrayAdapter<String>(this, R.layout.quick_response_item, mResponses));
80     }
81 
82     // implements OnItemClickListener
83     @Override
onItemClick(AdapterView<?> parent, View view, int position, long id)84     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
85 
86         String body = null;
87         if (mResponses != null && position < mResponses.length - 1) {
88             body = mResponses[position];
89         }
90 
91         // Start thread to query provider and send mail
92         new QueryThread(mEventId, body).start();
93     }
94 
95     private class QueryThread extends Thread {
96         long mEventId;
97         String mBody;
98 
QueryThread(long eventId, String body)99         QueryThread(long eventId, String body) {
100             mEventId = eventId;
101             mBody = body;
102         }
103 
104         @Override
run()105         public void run() {
106             Intent emailIntent = AlertReceiver.createEmailIntent(QuickResponseActivity.this,
107                     mEventId, mBody);
108             if (emailIntent != null) {
109                 try {
110                     startActivity(emailIntent);
111                     finish();
112                 } catch (ActivityNotFoundException ex) {
113                     QuickResponseActivity.this.getListView().post(new Runnable() {
114                         @Override
115                         public void run() {
116                             Toast.makeText(QuickResponseActivity.this,
117                                     R.string.quick_response_email_failed, Toast.LENGTH_LONG);
118                             finish();
119                         }
120                     });
121                 }
122             }
123         }
124     }
125 }
126