@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    final Context context = getApplicationContext();
    // Set a popup EditText view to get user input
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final EditText input = new EditText(this);
    alert.setView(input);

    switch (item.getItemId()) {
      case R.id.menu_settings_key:
        startActivity(new Intent(this, GpgPreferenceActivity.class));
        return true;
      case R.id.menu_create_key:
        startActivity(new Intent(this, CreateKeyActivity.class));
        return true;
      case R.id.menu_receive_key:
        alert.setTitle(R.string.receive_key);
        alert.setMessage(R.string.receive_key_message);
        alert.setPositiveButton(
            "Receive",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int whichButton) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                String ks = prefs.getString(GpgPreferenceActivity.PREF_KEYSERVER, "200.144.121.45");
                command =
                    NativeHelper.gpg2
                        + " --keyserver "
                        + ks
                        + " --recv-keys "
                        + input.getText().toString();
                commandThread = new CommandThread();
                commandThread.start();
              }
            });
        alert.show();
        return true;
      case R.id.menu_send_key:
        alert.setTitle(R.string.send_key);
        alert.setMessage(R.string.send_key_message);
        alert.setPositiveButton(
            "Send",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int whichButton) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                String ks = prefs.getString(GpgPreferenceActivity.PREF_KEYSERVER, "200.144.121.45");
                command =
                    NativeHelper.gpg2
                        + " --keyserver "
                        + ks
                        + " --send-keys "
                        + input.getText().toString();
                commandThread = new CommandThread();
                commandThread.start();
              }
            });
        alert.show();
        return true;
      case R.id.menu_list_keys:
        command = NativeHelper.gpg2 + "--list-secret-keys --fingerprint;";
        command += NativeHelper.gpg2 + "--list-keys --fingerprint;";
        commandThread = new CommandThread();
        commandThread.start();
        Log.i(TAG, "finished " + command);
        return true;
      case R.id.menu_change_passphrase:
        GnuPGKey[] keys = GnuPG.context.listSecretKeys();
        if (keys != null && keys.length > 0) {
          final GnuPGKey key = keys[0];
          String msg =
              String.format(
                  getString(R.string.changing_passphrase_for_format),
                  key.getName() + " <" + key.getEmail() + ">:" + key.getKeyID());
          Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
          new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
              GnuPG.context.changePassphrase(key);
              return null;
            }
          }.execute();
        } else {
          Toast.makeText(this, R.string.error_no_secret_key, Toast.LENGTH_LONG).show();
        }
        return true;
      case R.id.menu_run_test:
        command = NativeHelper.app_opt + "/tests/run-tests-with-password.sh";
        commandThread = new CommandThread();
        commandThread.start();
        Log.i(TAG, "finished " + command);
        return true;
      case R.id.menu_decrypt_file:
        final String decryptFilename =
            (NativeHelper.app_opt.getAbsolutePath() + "/tests/icon.png.gpg");
        showDecryptFile(decryptFilename);
        return true;
      case R.id.menu_import_key_from_file:
        final String defaultFilename =
            (NativeHelper.app_opt.getAbsolutePath() + "/tests/public-keys.pkr");
        showImportFromFileDialog(defaultFilename);
        return true;
      case R.id.menu_export_keys_to_file:
        final String exportFilename =
            Environment.getExternalStorageDirectory().getAbsolutePath() + "/gnupg-keyring.pkr";
        showExportToFileDialog(exportFilename);
        return true;
      case R.id.menu_share_log:
        shareTestLog();
        Log.i(TAG, "finished menu_share_log");
        return true;
    }
    return false;
  }
Exemple #2
0
 /**
  * Simultaneously refreshes the data objects listed in 'commands' from pazpar2, potentially
  * running a search or a record command first if any of these two commands have outstanding
  * parameter changes.
  *
  * @param commands, a comma-separated list of Pazpar2 commands to execute
  * @return Number of activeclients at the time of the 'show' command, or 'new' if search was just
  *     initiated.
  */
 public String update(String commands) {
   logger.debug("Request to update: " + commands);
   try {
     if (commands.equals("search")) {
       pzreq.getSearch().run();
       pzresp.getSearch().setIsNew(false);
       return "new";
     } else if (commands.equals("record")) {
       pzreq.getRecord().run();
       return pzresp.getRecord().getActiveClients();
     } else if (pzresp.getSearch().isNew()) {
       // For returning notification of 'search started' quickly to UI
       logger.info(
           "New search. Marking it old, then returning 'new' to trigger another round-trip.");
       pzresp.getSearch().setIsNew(false);
       return "new";
     } else {
       handleQueryStateChanges(commands);
       if (pzresp.getSearch().hasApplicationError()) {
         logger.error(
             "The command(s) "
                 + commands
                 + " cancelled because the latest search command had an error.");
         return "0";
       } else if (errors.hasConfigurationErrors()) {
         logger.error("The command(s) " + commands + " cancelled due to configuration errors.");
         return "0";
       } else {
         logger.debug("Processing request for " + commands);
         List<CommandThread> threadList = new ArrayList<CommandThread>();
         StringTokenizer tokens = new StringTokenizer(commands, ",");
         while (tokens.hasMoreElements()) {
           threadList.add(
               new CommandThread(
                   pzreq.getCommand(tokens.nextToken()),
                   searchClient,
                   Pz2Service.get().getPzresp()));
         }
         for (CommandThread thread : threadList) {
           thread.start();
         }
         for (CommandThread thread : threadList) {
           try {
             thread.join();
           } catch (InterruptedException e) {
             e.printStackTrace();
           }
         }
         return pzresp.getActiveClients();
       }
     }
   } catch (ClassCastException cce) {
     cce.printStackTrace();
     return "";
   } catch (NullPointerException npe) {
     npe.printStackTrace();
     return "";
   } catch (Exception e) {
     e.printStackTrace();
     return "";
   }
 }