1/*
2 * Copyright (C) 2013 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.browse;
19
20import android.content.ClipData;
21import android.content.ClipboardManager;
22import android.content.Context;
23import android.content.Intent;
24import android.net.Uri;
25import android.text.TextUtils;
26import android.view.ContextMenu;
27import android.view.MenuInflater;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.ContextMenu.ContextMenuInfo;
31import android.view.View.OnCreateContextMenuListener;
32import android.webkit.WebView;
33
34import com.android.mail.R;
35
36/**
37 * <p>
38 * Handles display and behavior for long clicking on expanded messages' headers.
39 * Requires a context to use for inflation and clipboard copying.
40 * </p>
41 * <br>
42 * Dependencies:
43 * <ul>
44 * <li>res/menu/email_copy_context_menu.xml</li>
45 * </ul>
46 */
47public class EmailCopyContextMenu implements OnCreateContextMenuListener{
48
49    // IDs for displaying in the menu
50    private static final int SEND_EMAIL_ITEM = R.id.mail_context_menu_id;
51    private static final int COPY_CONTACT_ITEM = R.id.copy_mail_context_menu_id;
52
53    // Reference to context for layout inflation & copying the email
54    private final Context mContext;
55    private CharSequence mAddress;
56
57    public EmailCopyContextMenu(Context context) {
58        mContext = context;
59    }
60
61    /**
62     * Copy class for handling click event on the "Copy" button and storing
63     * copied text.
64     */
65    private class Copy implements MenuItem.OnMenuItemClickListener {
66        private final CharSequence mText;
67
68        public Copy(CharSequence text) {
69            mText = text;
70        }
71
72        @Override
73        public boolean onMenuItemClick(MenuItem item) {
74            copy(mText);
75            //Handled & consumed event
76            return true;
77        }
78    }
79
80    /**
81     * Copy the input text sequence to the system clipboard.
82     * @param text CharSequence to be copied.
83     */
84    private void copy(CharSequence text) {
85        ClipboardManager clipboard =
86                (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
87        clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
88    }
89
90    public void setAddress(CharSequence address) {
91        this.mAddress = address;
92    }
93
94    /**
95     * Creates context menu via MenuInflater and populates with items defined
96     * in res/menu/email_copy_context_menu.xml
97     */
98    @Override
99    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo info) {
100        if(!TextUtils.isEmpty(mAddress)) {
101            MenuInflater inflater = new MenuInflater(mContext);
102            inflater.inflate(getMenuResourceId(), menu);
103
104            // Create menu and bind listener/intent
105            menu.setHeaderTitle(mAddress);
106            menu.findItem(SEND_EMAIL_ITEM).setIntent(new Intent(Intent.ACTION_VIEW,
107                    Uri.parse(WebView.SCHEME_MAILTO + mAddress)));
108            menu.findItem(COPY_CONTACT_ITEM).setOnMenuItemClickListener(
109                    new Copy(mAddress));
110        }
111    }
112
113    // Location of Context Menu layout
114    private static int getMenuResourceId() {
115        return R.menu.email_copy_context_menu;
116    }
117}
118