blob: 4b649d57fe33c6eadbc189591af29d01321762df [file] [log] [blame]
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -07001/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30package com.android.browser;
31
32import android.app.Activity;
33import android.app.AlertDialog;
34import android.content.ComponentName;
35import android.content.Context;
36import android.content.DialogInterface;
37import android.content.DialogInterface.OnClickListener;
38import android.content.Intent;
39import android.content.pm.ActivityInfo;
40import android.content.pm.ResolveInfo;
41import android.content.pm.PackageManager;
42import android.graphics.Bitmap;
43
44import java.util.List;
45import java.util.Collections;
46
47import android.util.Log;
48
49
50public class ShareDialog extends AppItem {
51 private Activity activity = null;
52 public String title = null;
53 public String url = null;
54 public Bitmap favicon = null;
55 public Bitmap screenshot = null;
56 private List<ResolveInfo>apps = null;
57 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
58 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
59
60
61 public ShareDialog (Activity activity, String title, String url, Bitmap favicon, Bitmap screenshot) {
62 super(null);
63 this.activity = activity;
64 this.apps = getShareableApps();
65 this.title = title;
66 this.url = url;
67 this.favicon = favicon;
68 this.screenshot = screenshot;
69 }
70
71 private List<ResolveInfo> getShareableApps() {
72 Intent shareIntent = new Intent("android.intent.action.SEND");
73 shareIntent.setType("text/plain");
74 PackageManager pm = activity.getPackageManager();
75 List<ResolveInfo> launchables = pm.queryIntentActivities(shareIntent, 0);
76
77 Collections.sort(launchables,
78 new ResolveInfo.DisplayNameComparator(pm));
79
80 return launchables;
81 }
82
83
84 public List<ResolveInfo> getApps() {
85 return apps;
86 }
87
88 public void loadView(final AppAdapter adapter) {
89 AlertDialog.Builder builderSingle = new AlertDialog.Builder(activity);
90 builderSingle.setIcon(R.mipmap.ic_launcher_browser_swe);
91 builderSingle.setTitle(activity.getString(R.string.choosertitle_sharevia));
92 builderSingle.setAdapter(adapter, new DialogInterface.OnClickListener() {
93 @Override
94 public void onClick(DialogInterface dialog, int position) {
95 dialog.dismiss();
96 ResolveInfo launchable = adapter.getItem(position);
97 ActivityInfo activityInfo = launchable.activityInfo;
98 ComponentName name = new android.content.ComponentName(activityInfo.applicationInfo.packageName,
99 activityInfo.name);
100 Intent i = new Intent(Intent.ACTION_SEND);
101 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
102 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
103 i.putExtra(Intent.EXTRA_TEXT, url);
104 i.putExtra(Intent.EXTRA_SUBJECT, title);
105 i.putExtra(EXTRA_SHARE_FAVICON, favicon);
106 i.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
107 i.setType("text/plain");
108 i.setComponent(name);
109 activity.startActivity(i);
110 }
111 });
112
113 builderSingle.show();
114 }
115}