1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.dialog
;
20 import java
.util
.Arrays
;
21 import java
.util
.Collections
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
25 import android
.app
.AlertDialog
;
26 import android
.app
.Dialog
;
27 import android
.content
.ComponentName
;
28 import android
.content
.Context
;
29 import android
.content
.DialogInterface
;
30 import android
.content
.Intent
;
31 import android
.content
.pm
.ActivityInfo
;
32 import android
.content
.pm
.PackageManager
;
33 import android
.content
.pm
.ResolveInfo
;
34 import android
.os
.Bundle
;
35 import android
.view
.LayoutInflater
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.ArrayAdapter
;
39 import android
.widget
.ImageView
;
40 import android
.widget
.TextView
;
42 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
43 import com
.owncloud
.android
.R
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
45 import com
.owncloud
.android
.files
.FileOperationsHelper
;
46 import com
.owncloud
.android
.ui
.activity
.CopyToClipboardActivity
;
47 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
48 import com
.owncloud
.android
.utils
.Log_OC
;
51 * Dialog showing a list activities able to resolve a given Intent,
52 * filtering out the activities matching give package names.
54 * @author David A. Velasco
56 public class ShareLinkToDialog
extends SherlockDialogFragment
{
58 private final static String TAG
= ShareLinkToDialog
.class.getSimpleName();
59 private final static String ARG_INTENT
= ShareLinkToDialog
.class.getSimpleName() + ".ARG_INTENT";
60 private final static String ARG_PACKAGES_TO_EXCLUDE
= ShareLinkToDialog
.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE";
61 private final static String ARG_FILE_TO_SHARE
= ShareLinkToDialog
.class.getSimpleName() + ".FILE_TO_SHARE";
63 private ActivityAdapter mAdapter
;
65 private Intent mIntent
;
67 public static ShareLinkToDialog
newInstance(Intent intent
, String
[] packagesToExclude
, OCFile fileToShare
) {
68 ShareLinkToDialog f
= new ShareLinkToDialog();
69 Bundle args
= new Bundle();
70 args
.putParcelable(ARG_INTENT
, intent
);
71 args
.putStringArray(ARG_PACKAGES_TO_EXCLUDE
, packagesToExclude
);
72 args
.putParcelable(ARG_FILE_TO_SHARE
, fileToShare
);
77 public ShareLinkToDialog() {
79 Log_OC
.d(TAG
, "constructor");
83 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
84 mIntent
= getArguments().getParcelable(ARG_INTENT
);
85 String
[] packagesToExclude
= getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE
);
86 List
<String
> packagesToExcludeList
= Arrays
.asList(packagesToExclude
!= null ? packagesToExclude
: new String
[0]);
87 mFile
= getArguments().getParcelable(ARG_FILE_TO_SHARE
);
89 PackageManager pm
= getSherlockActivity().getPackageManager();
90 List
<ResolveInfo
> activities
= pm
.queryIntentActivities(mIntent
, PackageManager
.MATCH_DEFAULT_ONLY
);
91 Iterator
<ResolveInfo
> it
= activities
.iterator();
92 ResolveInfo resolveInfo
;
93 while (it
.hasNext()) {
94 resolveInfo
= it
.next();
95 if (packagesToExcludeList
.contains(resolveInfo
.activityInfo
.packageName
.toLowerCase())) {
100 boolean sendAction
= mIntent
.getBooleanExtra(Intent
.ACTION_SEND
, false
);
103 // add activity for copy to clipboard
104 Intent copyToClipboardIntent
= new Intent(getSherlockActivity(), CopyToClipboardActivity
.class);
105 List
<ResolveInfo
> copyToClipboard
= pm
.queryIntentActivities(copyToClipboardIntent
, 0);
106 if (!copyToClipboard
.isEmpty()) {
107 activities
.add(copyToClipboard
.get(0));
111 Collections
.sort(activities
, new ResolveInfo
.DisplayNameComparator(pm
));
112 mAdapter
= new ActivityAdapter(getSherlockActivity(), pm
, activities
);
115 return new AlertDialog
.Builder(getSherlockActivity())
116 .setTitle(R
.string
.activity_chooser_send_file_title
)
117 .setAdapter(mAdapter
, new DialogInterface
.OnClickListener() {
119 public void onClick(DialogInterface dialog
, int which
) {
120 // Add the information of the chosen activity to the intent to send
121 ResolveInfo chosen
= mAdapter
.getItem(which
);
122 ActivityInfo actInfo
= chosen
.activityInfo
;
123 ComponentName name
=new ComponentName(actInfo
.applicationInfo
.packageName
, actInfo
.name
);
124 mIntent
.setComponent(name
);
127 FileOperationsHelper foh
= new FileOperationsHelper();
128 foh
.sendFileToApp(mIntent
, (FileActivity
)getSherlockActivity());
135 return new AlertDialog
.Builder(getSherlockActivity())
136 .setTitle(R
.string
.activity_chooser_title
)
137 .setAdapter(mAdapter
, new DialogInterface
.OnClickListener() {
139 public void onClick(DialogInterface dialog
, int which
) {
140 // Add the information of the chosen activity to the intent to send
141 ResolveInfo chosen
= mAdapter
.getItem(which
);
142 ActivityInfo actInfo
= chosen
.activityInfo
;
143 ComponentName name
=new ComponentName(actInfo
.applicationInfo
.packageName
, actInfo
.name
);
144 mIntent
.setComponent(name
);
146 // Create a new share resource
147 FileOperationsHelper foh
= new FileOperationsHelper();
148 foh
.shareFileWithLinkToApp(mFile
, mIntent
, (FileActivity
)getSherlockActivity());
156 class ActivityAdapter
extends ArrayAdapter
<ResolveInfo
> {
158 private PackageManager mPackageManager
;
160 ActivityAdapter(Context context
, PackageManager pm
, List
<ResolveInfo
> apps
) {
161 super(context
, R
.layout
.activity_row
, apps
);
162 this.mPackageManager
= pm
;
166 public View
getView(int position
, View convertView
, ViewGroup parent
) {
167 if (convertView
== null
) {
168 convertView
= newView(parent
);
170 bindView(position
, convertView
);
174 private View
newView(ViewGroup parent
) {
175 return(((LayoutInflater
)getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE
)).inflate(R
.layout
.activity_row
, parent
, false
));
178 private void bindView(int position
, View row
) {
179 TextView label
= (TextView
) row
.findViewById(R
.id
.title
);
180 label
.setText(getItem(position
).loadLabel(mPackageManager
));
181 ImageView icon
= (ImageView
) row
.findViewById(R
.id
.icon
);
182 icon
.setImageDrawable(getItem(position
).loadIcon(mPackageManager
));