Merge pull request #384 from owncloud/share_link__new_share_menu
[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.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.pm.PackageManager;
31 import android.content.pm.ResolveInfo;
32 import android.os.Bundle;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.ArrayAdapter;
37 import android.widget.ImageView;
38 import android.widget.ListAdapter;
39 import android.widget.TextView;
40
41 import com.actionbarsherlock.app.SherlockDialogFragment;
42 import com.owncloud.android.R;
43 import com.owncloud.android.utils.Log_OC;
44
45 /**
46 * Dialog showing a list activities able to resolve a given Intent,
47 * filtering out the activities matching give package names.
48 *
49 * @author David A. Velasco
50 */
51 public class ActivityChooserDialog extends SherlockDialogFragment {
52
53 private final static String TAG = ActivityChooserDialog.class.getSimpleName();
54 private final static String ARG_INTENT = ActivityChooserDialog.class.getSimpleName() + ".ARG_INTENT";
55 private final static String ARG_PACKAGES_TO_EXCLUDE = ActivityChooserDialog.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE";
56
57 private ListAdapter mAdapter;
58
59 public static ActivityChooserDialog newInstance(Intent intent, String[] packagesToExclude/*OnConflictDecisionMadeListener listener*/) {
60 ActivityChooserDialog f = new ActivityChooserDialog();
61 Bundle args = new Bundle();
62 args.putParcelable(ARG_INTENT, intent);
63 args.putStringArray(ARG_PACKAGES_TO_EXCLUDE, packagesToExclude);
64 f.setArguments(args);
65 return f;
66 }
67
68 public ActivityChooserDialog() {
69 super();
70 Log_OC.d(TAG, "constructor");
71 }
72
73 @Override
74 public Dialog onCreateDialog(Bundle savedInstanceState) {
75 Intent intent = getArguments().getParcelable(ARG_INTENT);
76 String[] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE);
77 List<String> packagesToExcludeList = Arrays.asList(packagesToExclude != null ? packagesToExclude : new String[0]);
78
79 PackageManager pm= getSherlockActivity().getPackageManager();
80 List<ResolveInfo> activities = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
81 Iterator<ResolveInfo> it = activities.iterator();
82 ResolveInfo resolveInfo;
83 while (it.hasNext()) {
84 resolveInfo = it.next();
85 if (packagesToExcludeList.contains(resolveInfo.activityInfo.packageName.toLowerCase())) {
86 it.remove();
87 }
88 }
89 Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
90 mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities);
91
92 return new AlertDialog.Builder(getSherlockActivity())
93 .setTitle(R.string.activity_chooser_title)
94 .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
95 @Override
96 public void onClick(DialogInterface dialog, int which) {
97 // The 'which' argument contains the index position
98 // of the selected item
99 }
100 })
101 .create();
102 }
103
104
105 class ActivityAdapter extends ArrayAdapter<ResolveInfo> {
106
107 private PackageManager mPackageManager;
108
109 ActivityAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
110 //super(context, android.R.layout.activity_list_item, apps);
111 super(context, R.layout.activity_row, apps);
112 this.mPackageManager = pm;
113 }
114
115 @Override
116 public View getView(int position, View convertView, ViewGroup parent) {
117 if (convertView == null) {
118 convertView = newView(parent);
119 }
120 bindView(position, convertView);
121 return convertView;
122 }
123
124 private View newView(ViewGroup parent) {
125 return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false));
126 //return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(android.R.layout.activity_list_item, parent, false));
127 }
128
129 private void bindView(int position, View row) {
130 TextView label = (TextView) row.findViewById(R.id.title);
131 //TextView label = (TextView) row.findViewById(android.R.id.text1);
132 label.setText(getItem(position).loadLabel(mPackageManager));
133 ImageView icon = (ImageView) row.findViewById(R.id.icon);
134 //ImageView icon = (ImageView) row.findViewById(android.R.id.icon);
135 icon.setImageDrawable(getItem(position).loadIcon(mPackageManager));
136 }
137 }
138
139 }