aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ShareHelper.java
blob: 58178e2961e1cdda844c261d2305035128e3626f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.sufficientlysecure.keychain.helper;

import android.content.Context;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.content.pm.ResolveInfo;
import android.os.Build;
/*
 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ShareHelper {
    Context mContext;

    public ShareHelper(Context context) {
        mContext = context;
    }

    /**
     * Create Intent Chooser but exclude OK's EncryptActivity.
     * <p/>
     * Put together from some stackoverflow posts...
     */
    public Intent createChooserExcluding(Intent prototype, String title, String[] activityBlacklist) {
        // Produced an empty list on Huawei U8860 with Android Version 4.0.3 and weird results on 2.3
        // TODO: test on 4.1, 4.2, 4.3, only tested on 4.4
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return Intent.createChooser(prototype, title);
        }

        List<LabeledIntent> targetedShareIntents = new ArrayList<LabeledIntent>();

        List<ResolveInfo> resInfoList = mContext.getPackageManager().queryIntentActivities(prototype, 0);
        List<ResolveInfo> resInfoListFiltered = new ArrayList<ResolveInfo>();
        if (!resInfoList.isEmpty()) {
            for (ResolveInfo resolveInfo : resInfoList) {
                // do not add blacklisted ones
                if (resolveInfo.activityInfo == null || Arrays.asList(activityBlacklist).contains(resolveInfo.activityInfo.name))
                    continue;

                resInfoListFiltered.add(resolveInfo);
            }

            if (!resInfoListFiltered.isEmpty()) {
                // sorting for nice readability
                Collections.sort(resInfoListFiltered, new Comparator<ResolveInfo>() {
                    @Override
                    public int compare(ResolveInfo first, ResolveInfo second) {
                        String firstName = first.loadLabel(mContext.getPackageManager()).toString();
                        String secondName = second.loadLabel(mContext.getPackageManager()).toString();
                        return firstName.compareToIgnoreCase(secondName);
                    }
                });

                // create the custom intent list
                for (ResolveInfo resolveInfo : resInfoListFiltered) {
                    Intent targetedShareIntent = (Intent) prototype.clone();
                    targetedShareIntent.setPackage(resolveInfo.activityInfo.packageName);
                    targetedShareIntent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);

                    LabeledIntent lIntent = new LabeledIntent(targetedShareIntent,
                            resolveInfo.activityInfo.packageName,
                            resolveInfo.loadLabel(mContext.getPackageManager()),
                            resolveInfo.activityInfo.icon);
                    targetedShareIntents.add(lIntent);
                }

                // Create chooser with only one Intent in it
                Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), title);
                // append all other Intents
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
                return chooserIntent;
            }

        }

        // fallback to Android's default chooser
        return Intent.createChooser(prototype, title);
    }
}