
package info.guardianproject.onionkit.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

public class OrbotHelper {

    private final static int REQUEST_CODE_STATUS = 100;

    public final static String ORBOT_PACKAGE_NAME = "org.torproject.android";
    public final static String ORBOT_MARKET_URI = "market://details?id=" + ORBOT_PACKAGE_NAME;
    public final static String ORBOT_FDROID_URI = "https://f-droid.org/repository/browse/?fdid="
            + ORBOT_PACKAGE_NAME;
    public final static String ORBOT_PLAY_URI = "https://play.google.com/store/apps/details?id="
            + ORBOT_PACKAGE_NAME;

    public final static String ACTION_START_TOR = "org.torproject.android.START_TOR";
    public final static String ACTION_REQUEST_HS = "org.torproject.android.REQUEST_HS_PORT";
    public final static int HS_REQUEST_CODE = 9999;

    private final static String FDROID_PACKAGE_NAME = "org.fdroid.fdroid";
    private final static String PLAY_PACKAGE_NAME = "com.android.vending";

    private OrbotHelper() {
        // only static utility methods, do not instantiate
    }

    public static boolean isOrbotRunning(Context context) {
        int procId = TorServiceUtils.findProcessId(context);

        return (procId != -1);
    }

    public static boolean isOrbotInstalled(Context context) {
        return isAppInstalled(context, ORBOT_PACKAGE_NAME);
    }

    private static boolean isAppInstalled(Context context, String uri) {
        try {
            PackageManager pm = context.getPackageManager();
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public static void requestHiddenServiceOnPort(Activity activity, int port) {
        Intent intent = new Intent(ACTION_REQUEST_HS);
        intent.setPackage(ORBOT_PACKAGE_NAME);
        intent.putExtra("hs_port", port);

        activity.startActivityForResult(intent, HS_REQUEST_CODE);
    }

    public static Intent getOrbotStartIntent() {
        Intent intent = new Intent(ACTION_START_TOR);
        intent.setPackage(ORBOT_PACKAGE_NAME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    public static Intent getOrbotInstallIntent(Context context) {
        final Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(ORBOT_MARKET_URI));

        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resInfos = pm.queryIntentActivities(intent, 0);

        String foundPackageName = null;
        for (ResolveInfo r : resInfos) {
            Log.i("OrbotHelper", "market: " + r.activityInfo.packageName);
            if (TextUtils.equals(r.activityInfo.packageName, FDROID_PACKAGE_NAME)
                    || TextUtils.equals(r.activityInfo.packageName, PLAY_PACKAGE_NAME)) {
                foundPackageName = r.activityInfo.packageName;
                break;
            }
        }

        if (foundPackageName == null) {
            intent.setData(Uri.parse(ORBOT_FDROID_URI));
        } else {
            intent.setPackage(foundPackageName);
        }
        return intent;
    }
}

@@ -1015,16 +1015,76 @@
 _PORT%22;%0A
+    public final static int START_TOR_RESULT = 0x048079234;%0A
     publ
@@ -2295,32 +2295,986 @@
 T_CODE);%0A    %7D%0A%0A
+    /**%0A     * First, checks whether Orbot is installed, then checks whether Orbot is%0A     * running. If Orbot is installed and not running, then an %7B@link Intent%7D is%0A     * sent to request Orbot to start. The result will be returned in%0A     * %7B@link Activity#onActivityResult(int requestCode, int resultCode, Intent data)%7D%0A     * with a %7B@code requestCode%7D of %7B@link START_TOR_RESULT%7D%0A     *%0A     * @param activity the %7B@link Activity%7D that gets the%0A     *            %7B@code START_TOR_RESULT%7D result%0A     * @return whether the start request was sent to Orbot%0A     */%0A    public static boolean requestStartTor(Activity activity) %7B%0A        if (OrbotHelper.isOrbotInstalled(activity)) %7B%0A            if (!OrbotHelper.isOrbotRunning(activity)) %7B%0A                Intent intent = getOrbotStartIntent();%0A                activity.startActivityForResult(intent, START_TOR_RESULT);%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A
     public stati

add OrbotHelper.requestStartTor() for easy sending of the start request