Merge branch 'develop' into share_link__unshare_file
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / ActivityChooserDialog.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.ui.dialog;
19
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
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;
41
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;
48
49 /**
50 * Dialog showing a list activities able to resolve a given Intent,
51 * filtering out the activities matching give package names.
52 *
53 * @author David A. Velasco
54 */
55 public class ActivityChooserDialog extends SherlockDialogFragment {
56
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";
61
62 private ActivityAdapter mAdapter;
63 private OCFile mFile;
64 private Intent mIntent;
65
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);
72 f.setArguments(args);
73 return f;
74 }
75
76 public ActivityChooserDialog() {
77 super();
78 Log_OC.d(TAG, "constructor");
79 }
80
81 @Override
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);
87
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())) {
95 it.remove();
96 }
97 }
98 Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
99 mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities);
100
101 return new AlertDialog.Builder(getSherlockActivity())
102 .setTitle(R.string.activity_chooser_title)
103 .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
104 @Override
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);
111
112 // Create a new share resource
113 FileOperationsHelper foh = new FileOperationsHelper();
114 foh.shareFileWithLinkToApp(mFile, mIntent, (FileActivity)getSherlockActivity());
115 }
116 })
117 .create();
118 }
119
120
121 class ActivityAdapter extends ArrayAdapter<ResolveInfo> {
122
123 private PackageManager mPackageManager;
124
125 ActivityAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
126 super(context, R.layout.activity_row, apps);
127 this.mPackageManager = pm;
128 }
129
130 @Override
131 public View getView(int position, View convertView, ViewGroup parent) {
132 if (convertView == null) {
133 convertView = newView(parent);
134 }
135 bindView(position, convertView);
136 return convertView;
137 }
138
139 private View newView(ViewGroup parent) {
140 return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false));
141 }
142
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));
148 }
149 }
150
151 }