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
.FileActivity
;
47 import com
.owncloud
.android
.utils
.Log_OC
;
50 * Dialog showing a list activities able to resolve a given Intent,
51 * filtering out the activities matching give package names.
53 * @author David A. Velasco
55 public class ActivityChooserDialog
extends SherlockDialogFragment
{
57 private final static String TAG
= ActivityChooserDialog
.class.getSimpleName();
58 private final static String ARG_INTENT
= ActivityChooserDialog
.class.getSimpleName() + ".ARG_INTENT";
59 private final static String ARG_PACKAGES_TO_EXCLUDE
= ActivityChooserDialog
.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE";
60 private final static String ARG_FILE_TO_SHARE
= ActivityChooserDialog
.class.getSimpleName() + ".FILE_TO_SHARE";
62 private ActivityAdapter mAdapter
;
64 private Intent mIntent
;
66 public static ActivityChooserDialog
newInstance(Intent intent
, String
[] packagesToExclude
, OCFile fileToShare
) {
67 ActivityChooserDialog f
= new ActivityChooserDialog();
68 Bundle args
= new Bundle();
69 args
.putParcelable(ARG_INTENT
, intent
);
70 args
.putStringArray(ARG_PACKAGES_TO_EXCLUDE
, packagesToExclude
);
71 args
.putParcelable(ARG_FILE_TO_SHARE
, fileToShare
);
76 public ActivityChooserDialog() {
78 Log_OC
.d(TAG
, "constructor");
82 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
83 mIntent
= getArguments().getParcelable(ARG_INTENT
);
84 String
[] packagesToExclude
= getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE
);
85 List
<String
> packagesToExcludeList
= Arrays
.asList(packagesToExclude
!= null ? packagesToExclude
: new String
[0]);
86 mFile
= getArguments().getParcelable(ARG_FILE_TO_SHARE
);
88 PackageManager pm
= getSherlockActivity().getPackageManager();
89 List
<ResolveInfo
> activities
= pm
.queryIntentActivities(mIntent
, PackageManager
.MATCH_DEFAULT_ONLY
);
90 Iterator
<ResolveInfo
> it
= activities
.iterator();
91 ResolveInfo resolveInfo
;
92 while (it
.hasNext()) {
93 resolveInfo
= it
.next();
94 if (packagesToExcludeList
.contains(resolveInfo
.activityInfo
.packageName
.toLowerCase())) {
98 Collections
.sort(activities
, new ResolveInfo
.DisplayNameComparator(pm
));
99 mAdapter
= new ActivityAdapter(getSherlockActivity(), pm
, activities
);
101 return new AlertDialog
.Builder(getSherlockActivity())
102 .setTitle(R
.string
.activity_chooser_title
)
103 .setAdapter(mAdapter
, new DialogInterface
.OnClickListener() {
105 public void onClick(DialogInterface dialog
, int which
) {
106 // Add the information of the chosen activity to the intent to send
107 ResolveInfo chosen
= mAdapter
.getItem(which
);
108 ActivityInfo actInfo
= chosen
.activityInfo
;
109 ComponentName name
=new ComponentName(actInfo
.applicationInfo
.packageName
, actInfo
.name
);
110 mIntent
.setComponent(name
);
112 // Create a new share resource
113 FileOperationsHelper foh
= new FileOperationsHelper();
114 foh
.shareFileWithLinkToApp(mFile
, mIntent
, (FileActivity
)getSherlockActivity());
121 class ActivityAdapter
extends ArrayAdapter
<ResolveInfo
> {
123 private PackageManager mPackageManager
;
125 ActivityAdapter(Context context
, PackageManager pm
, List
<ResolveInfo
> apps
) {
126 super(context
, R
.layout
.activity_row
, apps
);
127 this.mPackageManager
= pm
;
131 public View
getView(int position
, View convertView
, ViewGroup parent
) {
132 if (convertView
== null
) {
133 convertView
= newView(parent
);
135 bindView(position
, convertView
);
139 private View
newView(ViewGroup parent
) {
140 return(((LayoutInflater
)getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE
)).inflate(R
.layout
.activity_row
, parent
, false
));
143 private void bindView(int position
, View row
) {
144 TextView label
= (TextView
) row
.findViewById(R
.id
.title
);
145 label
.setText(getItem(position
).loadLabel(mPackageManager
));
146 ImageView icon
= (ImageView
) row
.findViewById(R
.id
.icon
);
147 icon
.setImageDrawable(getItem(position
).loadIcon(mPackageManager
));