AbstractMailActivity.java revision da7d9d4d472b4bc514c327a47265846b72f62fe8
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
18package com.android.mail.ui;
19
20import android.app.Activity;
21import android.content.Context;
22import android.nfc.NdefMessage;
23import android.nfc.NdefRecord;
24import android.nfc.NfcAdapter;
25import android.os.Bundle;
26import android.os.StrictMode;
27
28import java.io.UnsupportedEncodingException;
29import java.net.URLEncoder;
30
31/**
32 * <p>
33 * A complete Mail activity instance. This is the toplevel class that creates the views and handles
34 * the activity lifecycle.</p>
35 *
36 * <p>This class is abstract to allow many other activities to be quickly created by subclassing
37 * this activity and overriding a small subset of the life cycle methods: for example
38 * ComposeActivity and CreateShortcutActivity.</p>
39 *
40 * <p>In the Gmail codebase, this was called GmailBaseActivity</p>
41 *
42 */
43public abstract class AbstractMailActivity extends Activity
44        implements HelpCallback, RestrictedActivity {
45
46    private NfcAdapter mNfcAdapter; // final after onCreate
47    private NdefMessage mForegroundNdef;
48    private static AbstractMailActivity sForegroundInstance;
49    private final UiHandler mUiHandler = new UiHandler();
50
51    private static final boolean STRICT_MODE = false;
52
53    @Override
54    protected void onCreate(Bundle savedInstanceState) {
55        if (STRICT_MODE) {
56            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
57            .detectDiskReads()
58            .detectDiskWrites()
59            .detectAll()   // or .detectAll() for all detectable problems
60            .penaltyLog()
61            .build());
62            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
63            .detectLeakedSqlLiteObjects()
64            .detectLeakedClosableObjects()
65            .penaltyLog()
66            .penaltyDeath()
67            .build());
68        }
69
70        super.onCreate(savedInstanceState);
71
72        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
73        if (mNfcAdapter != null) {
74            // Find custom "from" address asynchronously.
75            // TODO(viki): Get a real account
76            String realAccount = "test@android.com";
77        }
78        mUiHandler.setEnabled(true);
79    }
80
81    @Override
82    protected void onStart() {
83        super.onStart();
84
85        mUiHandler.setEnabled(true);
86    }
87
88    @Override
89    protected void onSaveInstanceState(Bundle outState) {
90        super.onSaveInstanceState(outState);
91
92        mUiHandler.setEnabled(false);
93    }
94
95    @Override
96    protected void onPause() {
97        super.onPause();
98        synchronized (this) {
99            if (mNfcAdapter != null && mForegroundNdef != null) {
100                mNfcAdapter.disableForegroundNdefPush(this);
101            }
102            sForegroundInstance = null;
103        }
104    }
105
106    @Override
107    protected void onResume() {
108        super.onResume();
109        synchronized (this) {
110            sForegroundInstance = this;
111            if (mNfcAdapter != null && mForegroundNdef != null) {
112                mNfcAdapter.enableForegroundNdefPush(this, mForegroundNdef);
113            }
114        }
115
116        mUiHandler.setEnabled(true);
117    }
118
119    /**
120     * Sets an NDEF message to be shared with "zero-clicks" using NFC. The message
121     * will be available as long as the current activity is in the foreground.
122     */
123    static void setForegroundNdef(NdefMessage ndef) {
124        AbstractMailActivity foreground = sForegroundInstance;
125        if (foreground != null && foreground.mNfcAdapter != null) {
126            synchronized (foreground) {
127                foreground.mForegroundNdef = ndef;
128                if (sForegroundInstance != null) {
129                    if (ndef != null) {
130                        sForegroundInstance.mNfcAdapter.enableForegroundNdefPush(
131                                sForegroundInstance, ndef);
132                    } else {
133                        sForegroundInstance.mNfcAdapter.disableForegroundNdefPush(
134                                sForegroundInstance);
135                    }
136                }
137            }
138        }
139    }
140
141    /**
142     * Get the contextual help parameter for this activity. This can be overridden
143     * to allow the extending activities to return different help context strings.
144     * The default implementation is to return "gm".
145     * @return The help context of this activity.
146     */
147    @Override
148    public String getHelpContext() {
149        return "Mail";
150    }
151
152    /**
153     * Returns an NDEF message with a single mailto URI record
154     * for the given email address.
155     */
156    static NdefMessage getMailtoNdef(String account) {
157        byte[] accountBytes;
158        try {
159            accountBytes = URLEncoder.encode(account, "UTF-8").getBytes("UTF-8");
160        } catch (UnsupportedEncodingException e) {
161            accountBytes = account.getBytes();
162        }
163        byte prefix = 0x06; // mailto:
164        byte[] recordBytes = new byte[accountBytes.length + 1];
165        recordBytes[0] = prefix;
166        System.arraycopy(accountBytes, 0, recordBytes, 1, accountBytes.length);
167        NdefRecord mailto = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI,
168                new byte[0], recordBytes);
169        return new NdefMessage(new NdefRecord[] { mailto });
170    }
171
172    @Override
173    public Context getActivityContext() {
174        return this;
175    }
176}
177