Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2010 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.email.activity;
     18 
     19 import android.app.Activity;
     20 import android.content.ActivityNotFoundException;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.net.Uri;
     24 import android.provider.Browser;
     25 import android.view.WindowManager;
     26 
     27 import com.android.email.Controller;
     28 import com.android.email.Email;
     29 import com.android.email.R;
     30 import com.android.email.activity.setup.AccountSecurity;
     31 import com.android.emailcommon.provider.Account;
     32 import com.android.emailcommon.provider.Mailbox;
     33 import com.android.emailcommon.utility.EmailAsyncTask;
     34 import com.android.emailcommon.utility.Utility;
     35 
     36 /**
     37  * Various methods that are used by both 1-pane and 2-pane activities.
     38  *
     39  * Common code used by the activities and the fragments.
     40  */
     41 public final class ActivityHelper {
     42     private ActivityHelper() {
     43     }
     44 
     45     /**
     46      * Open an URL in a message.
     47      *
     48      * This is intended to mirror the operation of the original
     49      * (see android.webkit.CallbackProxy) with one addition of intent flags
     50      * "FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET".  This improves behavior when sublaunching
     51      * other apps via embedded URI's.
     52      *
     53      * We also use this hook to catch "mailto:" links and handle them locally.
     54      *
     55      * @param activity parent activity
     56      * @param url URL to open
     57      * @param senderAccountId if the URL is mailto:, we use this account as the sender.
     58      *        TODO When MessageCompose implements the account selector, this won't be necessary.
     59      *        Pass {@link Account#NO_ACCOUNT} to use the default account.
     60      * @return true if the URI has successfully been opened.
     61      */
     62     public static boolean openUrlInMessage(Activity activity, String url, long senderAccountId) {
     63         // hijack mailto: uri's and handle locally
     64         if (url != null && url.toLowerCase().startsWith("mailto:")) {
     65             return MessageCompose.actionCompose(activity, url, senderAccountId);
     66         }
     67 
     68         // Handle most uri's via intent launch
     69         boolean result = false;
     70         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
     71         intent.addCategory(Intent.CATEGORY_BROWSABLE);
     72         intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.getPackageName());
     73         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
     74         try {
     75             activity.startActivity(intent);
     76             result = true;
     77         } catch (ActivityNotFoundException ex) {
     78             // No applications can handle it.  Ignore.
     79         }
     80         return result;
     81     }
     82 
     83     /**
     84      * Open Calendar app with specific time
     85      */
     86     public static void openCalendar(Activity activity, long epochEventStartTime) {
     87         Uri uri = Uri.parse("content://com.android.calendar/time/" + epochEventStartTime);
     88         Intent intent = new Intent(Intent.ACTION_VIEW);
     89         intent.setData(uri);
     90         intent.putExtra("VIEW", "DAY");
     91         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
     92         activity.startActivity(intent);
     93     }
     94 
     95     public static void deleteMessage(Context context, long messageId) {
     96         Controller.getInstance(context).deleteMessage(messageId);
     97         Utility.showToast(context,
     98                 context.getResources().getQuantityString(R.plurals.message_deleted_toast, 1));
     99     }
    100 
    101     public static void moveMessages(final Context context, final long newMailboxId,
    102             final long[] messageIds) {
    103         Controller.getInstance(context).moveMessages(messageIds, newMailboxId);
    104         EmailAsyncTask.runAsyncSerial(new Runnable() {
    105             @Override
    106             public void run() {
    107                 String mailboxName = Mailbox.getDisplayName(context, newMailboxId);
    108                 if (mailboxName == null) {
    109                     return; // Mailbox gone??
    110                 }
    111                 String message = context.getResources().getQuantityString(
    112                         R.plurals.message_moved_toast, messageIds.length, messageIds.length ,
    113                         mailboxName);
    114                 Utility.showToast(context, message);
    115             }
    116         });
    117     }
    118 
    119     /**
    120      * If configured via debug flags, inhibit hardware graphics acceleration.  Must be called
    121      * early in onCreate().
    122      *
    123      * NOTE: Currently, this only works if HW accel is *not* enabled via the manifest.
    124      */
    125     public static void debugSetWindowFlags(Activity activity) {
    126         if (Email.sDebugInhibitGraphicsAcceleration) {
    127             // Clear the flag in the activity's window
    128             activity.getWindow().setFlags(0,
    129                     WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
    130         } else {
    131             // Set the flag in the activity's window
    132             activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
    133                     WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
    134         }
    135     }
    136 
    137     public static void showSecurityHoldDialog(Activity callerActivity, long accountId) {
    138         callerActivity.startActivity(
    139                 AccountSecurity.actionUpdateSecurityIntent(callerActivity, accountId, true));
    140     }
    141 
    142 }
    143