
package info.guardianproject.onionkit.ui;

import info.guardianproject.onionkit.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;

public class OrbotHelper {

    private final static int REQUEST_CODE_STATUS = 100;

    public final static String URI_ORBOT = "org.torproject.android";
    public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor";

    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 Context mContext = null;

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

    public boolean isOrbotRunning()
    {
        int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH);

        return (procId != -1);
    }

    public boolean isOrbotInstalled()
    {
        return isAppInstalled(URI_ORBOT);
    }

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

    public void promptToInstall(Activity activity)
    {
        String uriMarket = activity.getString(R.string.market_orbot);
        // show dialog - install from market, f-droid or direct APK
        showDownloadDialog(activity, activity.getString(R.string.install_orbot_),
                activity.getString(R.string.you_must_have_orbot),
                activity.getString(R.string.yes), activity.getString(R.string.no), uriMarket);
    }

    private static AlertDialog showDownloadDialog(final Activity activity,
            CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes,
            CharSequence stringButtonNo, final String uriString) {
        AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
        downloadDialog.setTitle(stringTitle);
        downloadDialog.setMessage(stringMessage);
        downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                Uri uri = Uri.parse(uriString);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                activity.startActivity(intent);
            }
        });
        downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        return downloadDialog.show();
    }

    public void requestOrbotStart(final Activity activity)
    {

        AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
        downloadDialog.setTitle(R.string.start_orbot_);
        downloadDialog
                .setMessage(R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_);
        downloadDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(URI_ORBOT);
                intent.setAction(ACTION_START_TOR);
                activity.startActivityForResult(intent, 1);
            }
        });
        downloadDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        downloadDialog.show();

    }

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

        activity.startActivityForResult(intent, HS_REQUEST_CODE);
    }
}

@@ -41,48 +41,8 @@
 i;%0A%0A
-import info.guardianproject.onionkit.R;%0A
 impo
@@ -66,16 +66,16 @@
 tivity;%0A
+
 import a
@@ -268,16 +268,57 @@
 t.Uri;%0A%0A
+import info.guardianproject.onionkit.R;%0A%0A
 public c
@@ -425,25 +425,34 @@
  String 
-URI_
 ORBOT
+_PACKAGE_NAME
  = %22org.
@@ -1140,25 +1140,34 @@
 stalled(
-URI_
 ORBOT
+_PACKAGE_NAME
 );%0A    %7D
@@ -2503,32 +2503,54 @@
 ickListener() %7B%0A
+            @Override%0A
             publ
@@ -2887,32 +2887,54 @@
 ickListener() %7B%0A
+            @Override%0A
             publ
@@ -3498,32 +3498,54 @@
 ickListener() %7B%0A
+            @Override%0A
             publ
@@ -3641,33 +3641,42 @@
  new Intent(
-URI_
 ORBOT
+_PACKAGE_NAME
 );%0A         
@@ -3890,32 +3890,54 @@
 ickListener() %7B%0A
+            @Override%0A
             publ
@@ -4176,17 +4176,26 @@
 ent(
-URI_
 ORBOT
+_PACKAGE_NAME
 );%0A 

rename URI_ORBOT to ORBOT_PACKAGE_NAME since its a packageName not a URI