1/*
2 * Copyright (C) 2014 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
18package com.android.mail.ui;
19
20import android.content.Intent;
21import android.net.Uri;
22import android.os.Bundle;
23import android.support.v7.app.ActionBar;
24import android.support.v7.app.AppCompatActivity;
25import android.view.Menu;
26import android.view.MenuItem;
27
28import com.android.mail.R;
29import com.android.mail.browse.ConversationAccountController;
30import com.android.mail.content.ObjectCursor;
31import com.android.mail.providers.Account;
32import com.android.mail.providers.UIProvider;
33import com.android.mail.utils.Utils;
34
35/**
36 * Activity that provides support for querying an {@link Account}
37 * as well as showing settings/help/send feedback in the action
38 * overflow menu.
39 */
40public abstract class AccountFeedbackActivity extends AppCompatActivity
41        implements ConversationAccountController, AccountLoadCallbacks.AccountLoadCallbackListener {
42    public static final String EXTRA_ACCOUNT_URI = "extra-account-uri";
43
44    private static final int ACCOUNT_LOADER = 0;
45
46    private static final String SAVED_ACCOUNT = "saved-account";
47
48    private MenuItem mHelpAndFeedbackItem;
49
50    protected Uri mAccountUri;
51    protected Account mAccount;
52
53    private AccountLoadCallbacks mAccountLoadCallbacks;
54
55    @Override
56    protected void onCreate(Bundle savedInstanceState) {
57        super.onCreate(savedInstanceState);
58        setContentView(R.layout.account_feedback_activity);
59
60        final ActionBar actionBar = getSupportActionBar();
61        actionBar.setDisplayHomeAsUpEnabled(true);
62
63        final Intent intent = getIntent();
64        mAccountUri = intent.getParcelableExtra(EXTRA_ACCOUNT_URI);
65
66        if (savedInstanceState != null) {
67            if (savedInstanceState.containsKey(SAVED_ACCOUNT)) {
68                mAccount = savedInstanceState.getParcelable(SAVED_ACCOUNT);
69            }
70        }
71
72        // Account uri will be null if we launched from outside of the app.
73        // So just don't load an account at all.
74        if (mAccountUri != null) {
75            mAccountLoadCallbacks = new AccountLoadCallbacks(
76                    this /* context */, mAccountUri, this /* accountLoadCallbackListener*/);
77            getLoaderManager().initLoader(ACCOUNT_LOADER, Bundle.EMPTY, mAccountLoadCallbacks);
78        }
79    }
80
81    @Override
82    public void onAccountLoadCallbackFinished(ObjectCursor<Account> data) {
83        if (data != null && data.moveToFirst()) {
84            mAccount = data.getModel();
85        }
86    }
87
88    @Override
89    public boolean onCreateOptionsMenu(Menu menu) {
90        if (mAccountUri == null) {
91            return false;
92        }
93
94        getMenuInflater().inflate(R.menu.account_feedback_menu, menu);
95        mHelpAndFeedbackItem = menu.findItem(R.id.help_info_menu_item);
96        return true;
97    }
98
99    @Override
100    public boolean onPrepareOptionsMenu(Menu menu) {
101        if (mHelpAndFeedbackItem != null) {
102            mHelpAndFeedbackItem.setVisible(mAccount != null
103                    && mAccount.supportsCapability(UIProvider.AccountCapabilities.HELP_CONTENT));
104        }
105
106        return super.onPrepareOptionsMenu(menu);
107    }
108
109    @Override
110    public boolean onOptionsItemSelected(MenuItem item) {
111        final int itemId = item.getItemId();
112        if (itemId == android.R.id.home) {
113            finish();
114        } else if (itemId == R.id.settings) {
115            Utils.showSettings(this, mAccount);
116        } else if (itemId == R.id.help_info_menu_item) {
117            showHelp(getString(R.string.main_help_context));
118        } else {
119            return super.onOptionsItemSelected(item);
120        }
121
122        return true;
123    }
124
125    @Override
126    public Account getAccount() {
127        return mAccount;
128    }
129
130    protected void showHelp(String helpContext) {
131        Utils.showHelp(this, mAccount, helpContext);
132    }
133}
134