Android Open Source - Android-MarketBin Market Locator






From Project

Back to project page Android-MarketBin.

License

The source code is released under:

MIT License

If you think the Android project Android-MarketBin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.haunted.marketbin;
// www .  j av a  2s. com
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class MarketLocator {
    private final PackageManager packageManager;
    private List<PackageInfo> packages;


    public MarketLocator(Context context) {
        packageManager = context.getPackageManager();
    }

    public boolean isAppInstalled(String packageName) {
        if (packages == null) {
            if (packageManager == null)
                throw new NullPointerException("Can't get PackageManager");
            packages = packageManager.getInstalledPackages(0);
        }
        if (packages != null && packageName != null) {
            for (PackageInfo packageInfo : packages) {
                if (packageName.equals(packageInfo.packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isIntentAvailable(String appUrl) {
        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(appUrl));
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                        PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

    public IMarketDescriptor findByPackage(Iterable<? extends IMarketDescriptor> orderedMarketsList) {
        for (IMarketDescriptor m: orderedMarketsList) {
            if (m != null && isAppInstalled(m.getPackageName()))
                return m;
        }
        return null;
    }

    public IMarketDescriptor findByIntent(Iterable<? extends IMarketDescriptor> orderedMarketsList) {
        for (IMarketDescriptor m: orderedMarketsList) {
            if (m != null && isIntentAvailable(m.getIntentFormat()))
                return m;
        }
        return null;
    }

    public IMarketDescriptor findFirst(Iterable<? extends IMarketDescriptor> orderedMarketsList) {
        IMarketDescriptor result = findByPackage(orderedMarketsList);
        if (result != null)
            return result;
        return findByIntent(orderedMarketsList);
    }

    // WARNING: Does not check if application is present in listed AppMarkets, only checks if
    // market is available on current device.
    public List<IMarketDescriptor> findAll(Iterable<? extends IMarketDescriptor> orderedMarketsList,
                                          boolean checkPackage, boolean checkIntent) {
        List<IMarketDescriptor> result = new ArrayList<IMarketDescriptor>();
        List<String> schemesFound = new ArrayList<String>();
        if (checkPackage) {
            for (IMarketDescriptor m: orderedMarketsList)
                if (isAppInstalled(m.getPackageName())) {
                    result.add(m);
                    schemesFound.add(m.getIntentFormat());
                }
        }
        if (checkIntent) {
            for (IMarketDescriptor m: orderedMarketsList) {
                String scheme = m.getIntentFormat();
                if (!result.contains(m) && !schemesFound.contains(scheme) && isIntentAvailable(scheme)) {
                    result.add(m);
                    schemesFound.add(scheme);
                }
            }
        }
        return result;
    }
}




Java Source Code List

com.haunted.marketbin.IMarketDescriptor.java
com.haunted.marketbin.MarketBin.java
com.haunted.marketbin.MarketDescriptor.java
com.haunted.marketbin.MarketLocator.java
com.haunted.marketbin.MarketUI.java
com.haunted.marketbin.sample.FragmentCredits.java
com.haunted.marketbin.sample.FragmentMultiple.java
com.haunted.marketbin.sample.FragmentSingle.java
com.haunted.marketbin.sample.MainActivity.java
com.haunted.marketbin.sample.PackageDialogFragment.java