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