Home | History | Annotate | Download | only in ui
      1 /**
      2  * Copyright (c) 2014, Google Inc.
      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 package com.android.mail.ui;
     17 
     18 import android.annotation.TargetApi;
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.DialogFragment;
     23 import android.app.Fragment;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.net.Uri;
     27 import android.os.Bundle;
     28 import android.print.PrintAttributes;
     29 import android.print.PrintDocumentAdapter;
     30 import android.print.PrintManager;
     31 import android.support.v4.print.PrintHelper;
     32 import android.text.TextUtils;
     33 import android.view.LayoutInflater;
     34 import android.view.Menu;
     35 import android.view.MenuInflater;
     36 import android.view.MenuItem;
     37 import android.view.View;
     38 import android.view.ViewGroup;
     39 import android.webkit.WebView;
     40 import android.webkit.WebViewClient;
     41 
     42 import com.android.mail.R;
     43 
     44 import java.util.Calendar;
     45 
     46 /**
     47  * This fragment shows the Help screen.
     48  */
     49 public final class HelpFragment extends Fragment {
     50 
     51     /** Displays the copyright information, privacy policy or open source licenses. */
     52     private WebView mWebView;
     53 
     54     // Public no-args constructor needed for fragment re-instantiation
     55     public HelpFragment() {
     56     }
     57 
     58     @Override
     59     public void onActivityCreated(Bundle savedInstanceState) {
     60         super.onActivityCreated(savedInstanceState);
     61 
     62         final Uri helpUri = getActivity().getIntent()
     63                 .getParcelableExtra(HelpActivity.PARAM_HELP_URL);
     64         mWebView.loadUrl(helpUri.toString());
     65     }
     66 
     67     @Override
     68     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
     69         setHasOptionsMenu(true);
     70 
     71         final View view = inflater.inflate(R.layout.help_fragment, container, false);
     72         if (view != null) {
     73             mWebView = (WebView) view.findViewById(R.id.webview);
     74             mWebView.setWebViewClient(new WebViewClient());
     75             if (state != null) {
     76                 mWebView.restoreState(state);
     77             }
     78         }
     79 
     80         return view;
     81     }
     82 
     83     @Override
     84     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
     85         inflater.inflate(R.menu.help_menu, menu);
     86 
     87         // if we have no play store URI, hide that menu item
     88         final MenuItem viewAppUrlMenuItem = menu.findItem(R.id.view_app_url);
     89         if (viewAppUrlMenuItem != null) {
     90             final String appUrl = getString(R.string.app_url);
     91             viewAppUrlMenuItem.setVisible(!TextUtils.isEmpty(appUrl));
     92         }
     93 
     94         // printing the content of the webview is only allowed if running on Kitkat or later
     95         final MenuItem printItem = menu.findItem(R.id.print_dialog);
     96         if (printItem != null) {
     97             printItem.setVisible(PrintHelper.systemSupportsPrint());
     98         }
     99     }
    100 
    101     @Override
    102     public boolean onOptionsItemSelected(MenuItem item) {
    103         final int itemId = item.getItemId();
    104         if (itemId == android.R.id.home) {
    105             final Activity activity = getActivity();
    106             if (activity != null) {
    107                 activity.finish();
    108             }
    109             return true;
    110         } else if (itemId == R.id.view_app_url) {
    111             showAppUrl();
    112             return true;
    113         } else if (itemId == R.id.print_dialog) {
    114             print();
    115             return true;
    116         } else if (itemId == R.id.copyright_information) {
    117             showCopyrightInformation();
    118             return true;
    119         } else if (itemId == R.id.open_source_licenses) {
    120             showOpenSourceLicenses();
    121             return true;
    122         } else if (itemId == R.id.privacy_policy) {
    123             showPrivacyPolicy();
    124             return true;
    125         }
    126         return super.onOptionsItemSelected(item);
    127     }
    128 
    129     @TargetApi(19)
    130     private void print() {
    131         // pick a name for the print job we will create
    132         final String title = getActivity().getActionBar().getTitle().toString();
    133         final String jobName = getString(R.string.print_job_name, title);
    134 
    135         // ask the webview for a print adapter
    136         final PrintDocumentAdapter pda = mWebView.createPrintDocumentAdapter();
    137 
    138         // ask the print manager to print the contents of the webview using the job name
    139         final PrintManager pm = (PrintManager) getActivity().
    140                 getSystemService(Context.PRINT_SERVICE);
    141         pm.print(jobName, pda, new PrintAttributes.Builder().build());
    142     }
    143 
    144     private void showAppUrl() {
    145         final Uri uri = Uri.parse(getString(R.string.app_url));
    146         final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    147         startActivity(intent);
    148     }
    149 
    150     private void showCopyrightInformation() {
    151         new CopyrightDialogFragment().show(getFragmentManager(), "copyright");
    152     }
    153 
    154     private void showOpenSourceLicenses() {
    155         final Context context = getActivity();
    156         final Intent intent = new Intent(context, LicensesActivity.class);
    157         context.startActivity(intent);
    158     }
    159 
    160     private void showPrivacyPolicy() {
    161         final Uri uri = Uri.parse(getString(R.string.privacy_policy_uri));
    162         final Intent i = new Intent(Intent.ACTION_VIEW, uri);
    163         startActivity(i);
    164     }
    165 
    166     public static class CopyrightDialogFragment extends DialogFragment {
    167 
    168         public CopyrightDialogFragment() {
    169         }
    170 
    171         @Override
    172         public Dialog onCreateDialog(Bundle savedInstanceState) {
    173             // generate and display a copyright statement resembling " Google 2014"
    174             final int year = Calendar.getInstance().get(Calendar.YEAR);
    175             final String copyright = getString(R.string.copyright, year);
    176 
    177             return new AlertDialog.Builder(getActivity())
    178                     .setMessage(copyright)
    179                     .setNegativeButton(R.string.cancel, null).create();
    180         }
    181     }
    182 }
    183