Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 30 | package com.android.browser; |
| 31 | |
| 32 | import android.app.Activity; |
| 33 | import android.app.AlertDialog; |
| 34 | import android.content.ComponentName; |
| 35 | import android.content.Context; |
| 36 | import android.content.DialogInterface; |
| 37 | import android.content.DialogInterface.OnClickListener; |
| 38 | import android.content.Intent; |
| 39 | import android.content.pm.ActivityInfo; |
| 40 | import android.content.pm.ResolveInfo; |
| 41 | import android.content.pm.PackageManager; |
| 42 | import android.graphics.Bitmap; |
Axesh R. Ajmera | 89cf4d8 | 2015-05-19 11:26:18 -0700 | [diff] [blame] | 43 | import android.os.Build; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 44 | |
| 45 | import java.util.List; |
| 46 | import java.util.Collections; |
| 47 | |
| 48 | import android.util.Log; |
| 49 | |
| 50 | |
| 51 | public class ShareDialog extends AppItem { |
| 52 | private Activity activity = null; |
| 53 | public String title = null; |
| 54 | public String url = null; |
| 55 | public Bitmap favicon = null; |
| 56 | public Bitmap screenshot = null; |
| 57 | private List<ResolveInfo>apps = null; |
| 58 | public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot"; |
| 59 | public final static String EXTRA_SHARE_FAVICON = "share_favicon"; |
| 60 | |
| 61 | |
| 62 | public ShareDialog (Activity activity, String title, String url, Bitmap favicon, Bitmap screenshot) { |
| 63 | super(null); |
| 64 | this.activity = activity; |
| 65 | this.apps = getShareableApps(); |
| 66 | this.title = title; |
| 67 | this.url = url; |
| 68 | this.favicon = favicon; |
| 69 | this.screenshot = screenshot; |
| 70 | } |
| 71 | |
| 72 | private List<ResolveInfo> getShareableApps() { |
| 73 | Intent shareIntent = new Intent("android.intent.action.SEND"); |
| 74 | shareIntent.setType("text/plain"); |
| 75 | PackageManager pm = activity.getPackageManager(); |
| 76 | List<ResolveInfo> launchables = pm.queryIntentActivities(shareIntent, 0); |
| 77 | |
| 78 | Collections.sort(launchables, |
| 79 | new ResolveInfo.DisplayNameComparator(pm)); |
| 80 | |
| 81 | return launchables; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | public List<ResolveInfo> getApps() { |
| 86 | return apps; |
| 87 | } |
| 88 | |
| 89 | public void loadView(final AppAdapter adapter) { |
| 90 | AlertDialog.Builder builderSingle = new AlertDialog.Builder(activity); |
Tarun Nainani | 82ca1bd | 2015-07-16 14:31:17 -0700 | [diff] [blame] | 91 | builderSingle.setIcon(R.mipmap.ic_launcher_browser); |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 92 | builderSingle.setTitle(activity.getString(R.string.choosertitle_sharevia)); |
| 93 | builderSingle.setAdapter(adapter, new DialogInterface.OnClickListener() { |
| 94 | @Override |
| 95 | public void onClick(DialogInterface dialog, int position) { |
| 96 | dialog.dismiss(); |
| 97 | ResolveInfo launchable = adapter.getItem(position); |
| 98 | ActivityInfo activityInfo = launchable.activityInfo; |
| 99 | ComponentName name = new android.content.ComponentName(activityInfo.applicationInfo.packageName, |
| 100 | activityInfo.name); |
| 101 | Intent i = new Intent(Intent.ACTION_SEND); |
Axesh R. Ajmera | 89cf4d8 | 2015-05-19 11:26:18 -0700 | [diff] [blame] | 102 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { |
| 103 | // This flag clears the called app from the activity stack, |
| 104 | // so users arrive in the expected place next time this application is restarted |
| 105 | i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
| 106 | } else { |
| 107 | // flag used from Lollipop onwards |
| 108 | i.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 109 | } |
| 110 | |
| 111 | i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | |
| 112 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
| 113 | i.setType("text/plain"); |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 114 | i.putExtra(Intent.EXTRA_TEXT, url); |
| 115 | i.putExtra(Intent.EXTRA_SUBJECT, title); |
| 116 | i.putExtra(EXTRA_SHARE_FAVICON, favicon); |
| 117 | i.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot); |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 118 | i.setComponent(name); |
| 119 | activity.startActivity(i); |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | builderSingle.show(); |
| 124 | } |
Tarun Nainani | 82ca1bd | 2015-07-16 14:31:17 -0700 | [diff] [blame] | 125 | } |