1/*
2 * Copyright (C) 2015 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.messaging.ui;
18
19import android.app.Fragment;
20import android.appwidget.AppWidgetManager;
21import android.content.Intent;
22import android.os.Bundle;
23
24import com.android.messaging.Factory;
25import com.android.messaging.datamodel.data.ConversationListItemData;
26import com.android.messaging.ui.conversationlist.ShareIntentFragment;
27import com.android.messaging.util.Assert;
28import com.android.messaging.util.BuglePrefs;
29import com.android.messaging.widget.WidgetConversationProvider;
30
31public class WidgetPickConversationActivity extends BaseBugleActivity implements
32        ShareIntentFragment.HostInterface {
33
34    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
35
36    @Override
37    protected void onCreate(final Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39
40
41        // Set the result to CANCELED.  This will cause the widget host to cancel
42        // out of the widget placement if they press the back button.
43        setResult(RESULT_CANCELED);
44
45        // Find the widget id from the intent.
46        final Intent intent = getIntent();
47        final Bundle extras = intent.getExtras();
48        if (extras != null) {
49            mAppWidgetId = extras.getInt(
50                    AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
51        }
52
53        // If they gave us an intent without the widget id, just bail.
54        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
55            finish();
56        }
57
58        final ShareIntentFragment convPicker = new ShareIntentFragment();
59        final Bundle bundle = new Bundle();
60        bundle.putBoolean(ShareIntentFragment.HIDE_NEW_CONVERSATION_BUTTON_KEY, true);
61        convPicker.setArguments(bundle);
62        convPicker.show(getFragmentManager(), "ShareIntentFragment");
63    }
64
65    @Override
66    public void onAttachFragment(final Fragment fragment) {
67        final Intent intent = getIntent();
68        final String action = intent.getAction();
69        if (!AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)) {
70            // Unsupported action.
71            Assert.fail("Unsupported action type: " + action);
72        }
73    }
74
75    @Override
76    public void onConversationClick(final ConversationListItemData conversationListItemData) {
77        saveConversationidPref(mAppWidgetId, conversationListItemData.getConversationId());
78
79        // Push widget update to surface with newly set prefix
80        WidgetConversationProvider.rebuildWidget(this, mAppWidgetId);
81
82        // Make sure we pass back the original appWidgetId
83        Intent resultValue = new Intent();
84        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
85        setResult(RESULT_OK, resultValue);
86        finish();
87    }
88
89    @Override
90    public void onCreateConversationClick() {
91        // We should never get here because we're hiding the new conversation button in the
92        // ShareIntentFragment by setting HIDE_NEW_CONVERSATION_BUTTON_KEY in the arguments.
93        finish();
94    }
95
96    // Write the ConversationId to the SharedPreferences object for this widget
97    static void saveConversationidPref(int appWidgetId, String conversationId) {
98        final BuglePrefs prefs = Factory.get().getWidgetPrefs();
99        prefs.putString(UIIntents.UI_INTENT_EXTRA_CONVERSATION_ID + appWidgetId, conversationId);
100    }
101
102    // Read the ConversationId from the SharedPreferences object for this widget.
103    public static String getConversationIdPref(int appWidgetId) {
104        final BuglePrefs prefs = Factory.get().getWidgetPrefs();
105        return prefs.getString(UIIntents.UI_INTENT_EXTRA_CONVERSATION_ID + appWidgetId, null);
106    }
107
108    // Delete the ConversationId preference from the SharedPreferences object for this widget.
109    public static void deleteConversationIdPref(int appWidgetId) {
110        final BuglePrefs prefs = Factory.get().getWidgetPrefs();
111        prefs.remove(UIIntents.UI_INTENT_EXTRA_CONVERSATION_ID + appWidgetId);
112    }
113
114}
115