Beispiel #1
0
  @Override
  public void onResume() {
    super.onResume();

    informaCam = (InformaCam) getApplication();
    informaCam.setStatusListener(this);

    // Log.d(LOG, "AND HELLO onResume()!!");

    try {
      if (route != null) {
        routeByIntent();
      } else {
        Log.d(LOG, "route is null now, please wait");
        Log.d(LOG, "hasCredentialManager? " + String.valueOf(informaCam.hasCredentialManager()));

        if (informaCam.hasCredentialManager()) {
          Log.d(LOG, "NOW ASKING FOR CM STATUS...");

          switch (informaCam.getCredentialManagerStatus()) {
            case org.witness.informacam.utils.Constants.Codes.Status.UNLOCKED:
              route = new Intent(this, HomeActivity.class);

              if (prefStealthIcon) route.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

              routeCode = Home.ROUTE_CODE;
              break;
            case org.witness.informacam.utils.Constants.Codes.Status.LOCKED:
              route = new Intent(this, LoginActivity.class);
              routeCode = Login.ROUTE_CODE;
              break;
          }

          routeByIntent();
        } else {
          Log.d(LOG, "no, not logged in");
        }
      }

    } catch (NullPointerException e) {
      Logger.e(LOG, e);
    }
  }
  @Override
  public void onResume() {
    super.onResume();

    try {
      for (String s : InformaCam.getInstance().getAssets().list("includedOrganizations")) {
        Log.d(LOG, "NEW ASSET: " + s);
      }
    } catch (IOException e) {
      Log.e(LOG, e.toString());
      e.printStackTrace();
    }
  }
 public boolean save() {
   synchronized (this) {
     return InformaCam.getInstance().saveState(this);
   }
 }
Beispiel #4
0
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    informaCam.setStatusListener(this);

    if (resultCode == Activity.RESULT_CANCELED) {

      if (informaCam.isOutsideTheLoop(init.getAction())) {
        // Logger.d(LOG, "coming back from VMM call WITH NOOOOO MEDIA, and i shoudl finish.");
        setResult(resultCode, getIntent());
        finish();
        return;
      } else if (data != null
          && data.hasExtra(Codes.Extras.LOGOUT_USER)
          && data.getBooleanExtra(Codes.Extras.LOGOUT_USER, false)) {
        Logger.d(LOG, "Logout the user and close.");
        informaCam.setStatusListener(null);
        // informaCam.stopInforma();
        route = null;
        setResult(resultCode, getIntent());
        finish();

        if (data.hasExtra(Codes.Extras.PERFORM_WIPE)
            && data.getBooleanExtra(Codes.Extras.PERFORM_WIPE, false)) {
          wipe();
        }

        return;
      }

      // XXX: DOES THIS BREAK LOGOUT?
      setResult(resultCode, data);
      finish();

    } else if (resultCode == Activity.RESULT_OK) {
      Log.d(LOG, "returning with request code " + requestCode);

      /*
      if(informaCam.isOutsideTheLoop(init.getAction())) {
      	Logger.d(LOG, "coming back from VMM call with SOME media, and i shoudl finish.");

      	// TODO:
      	 immediately
      	 1) chooser
      	 2) encrypt (all? selected?) to org
      	 3) start up a transport for each returned media

      	finish();
      	return;
      }
      */

      route = new Intent(this, HomeActivity.class);
      if (prefStealthIcon) route.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

      routeCode = Home.ROUTE_CODE;

      switch (requestCode) {
        case Codes.Routes.CAMERA:
          route = new Intent(this, EditorActivity.class);
          routeCode = Editor.ROUTE_CODE;

          break;
        case Codes.Routes.EDITOR:
          break;
        case Codes.Routes.LOGIN:
          break;
        case Codes.Routes.HOME:
          if (data != null && data.hasExtra(Codes.Extras.CHANGE_LOCALE)) {
            route.putExtra(Codes.Extras.CHANGE_LOCALE, true);
          }

          break;
        case Codes.Routes.WIZARD:
          route = new Intent(this, HomeActivity.class);
          if (prefStealthIcon) route.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

          routeCode = Home.ROUTE_CODE;
          route.putExtra(Codes.Extras.GENERATING_KEY, true);

          if (KeyUtility.initDevice()) {

            informaCam.user.hasCompletedWizard = true;
            informaCam.user.lastLogIn = System.currentTimeMillis();
            informaCam.user.isLoggedIn = true;

            informaCam.saveState(informaCam.user);
          }

          break;
      }

      routeByIntent();
    } else if (resultCode == Activity.RESULT_FIRST_USER) {
      if (data.hasExtra(Codes.Extras.CHANGE_LOCALE)
          && data.getBooleanExtra(Codes.Extras.CHANGE_LOCALE, false)) {
        route.putExtra(Codes.Extras.CHANGE_LOCALE, true);
      }
    }
  }
  @Override
  public void wizardCompleted() {
    // save everything and finish

    InformaCam informaCam = (InformaCam) getApplication();

    informaCam.user.hasCompletedWizard = true;
    informaCam.user.lastLogIn = System.currentTimeMillis();
    informaCam.user.isLoggedIn = true;

    informaCam.saveState(informaCam.user);
    informaCam.saveState(informaCam.languageMap);

    try {
      informaCam.initData();
    } catch (Exception re) {
      throw new RuntimeException("Could not init data", re);
    }

    try {
      for (String s : informaCam.getAssets().list("includedOrganizations")) {

        InputStream ictdIS =
            informaCam.ioService.getStream("includedOrganizations/" + s, Type.APPLICATION_ASSET);

        byte[] ictdBytes = new byte[ictdIS.available()];
        ictdIS.read(ictdBytes);

        IOrganization organization =
            informaCam.installICTD(
                (JSONObject) new JSONTokener(new String(ictdBytes)).nextValue(), null, this);
        if (organization != null && !informaCam.user.isInOfflineMode) {
          INotification notification =
              new INotification(
                  getResources().getString(R.string.key_sent),
                  getResources()
                      .getString(
                          R.string.you_have_sent_your_credentials_to_x,
                          organization.organizationName),
                  Models.INotification.Type.NEW_KEY);
          notification.taskComplete = false;
          informaCam.addNotification(notification, null);

          ITransportStub transportStub = new ITransportStub(organization, notification);
          transportStub.setAsset(
              IUser.PUBLIC_CREDENTIALS,
              IUser.PUBLIC_CREDENTIALS,
              MimeType.ZIP,
              Storage.Type.IOCIPHER);
          transportStub.callbackCode =
              Models.ITransportStub.CallbackCodes.UPDATE_ORGANIZATION_HAS_KEY;

          TransportUtility.initTransport(transportStub);
        }
      }
    } catch (IOException e) {
      Log.e(LOG, e.toString());
      e.printStackTrace();
    } catch (JSONException e) {
      Log.e(LOG, e.toString());
      e.printStackTrace();
    }

    if (getIntent().getExtras() != null && getIntent().getExtras().size() > 0) {
      Iterator<String> it = getIntent().getExtras().keySet().iterator();
      while (it.hasNext()) {
        getIntent().removeExtra(it.next());
      }
    }

    setResult(Activity.RESULT_OK);
    finish();
  }
public class SurfaceGrabberActivity extends Activity
    implements OnClickListener, SurfaceHolder.Callback, PictureCallback {
  Button button;
  TextView progress;

  SurfaceView view;
  SurfaceHolder holder;
  Camera camera;
  CameraInfo cameraInfo;

  private InformaCam informaCam = InformaCam.getInstance();
  private List<String> baseImages = new ArrayList<String>();
  private boolean mPreviewing;

  private static final String LOG = App.ImageCapture.LOG;

  private int mRotation = 0;

  @SuppressWarnings("deprecation")
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayout());

    button = (Button) findViewById(R.id.surface_grabber_button);
    button.setOnClickListener(this);

    progress = (TextView) findViewById(R.id.surface_grabber_progress);
    progress.setText(String.valueOf(baseImages.size()));

    view = (SurfaceView) findViewById(R.id.surface_grabber_holder);
    view.setOnClickListener(this);

    holder = view.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }

  protected int getLayout() {
    return R.layout.activity_surface_grabber;
  }

  protected int getCameraDirection() {
    return CameraInfo.CAMERA_FACING_BACK;
  }

  /**
   * Whether or not we can default to "other" direction if our preferred facing camera can't be
   * opened
   *
   * @return true to try camera facing other way, false otherwise
   */
  protected boolean canUseOtherDirection() {
    return false;
  }

  @Override
  public void onResume() {
    super.onResume();

    if (!tryCreateCamera(getCameraDirection())) {
      if (!canUseOtherDirection() || !tryCreateCamera(getOtherDirection(getCameraDirection()))) {
        finish();
        return;
      }
    }

    if (camera == null) finish();

    setCameraDisplayOrientation();
  }

  private int getOtherDirection(int facing) {
    return (facing == CameraInfo.CAMERA_FACING_BACK)
        ? CameraInfo.CAMERA_FACING_FRONT
        : CameraInfo.CAMERA_FACING_BACK;
  }

  private boolean tryCreateCamera(int facing) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    for (int nCam = 0; nCam < Camera.getNumberOfCameras(); nCam++) {
      Camera.getCameraInfo(nCam, info);
      if (info.facing == facing) {
        camera = Camera.open(nCam);
        cameraInfo = info;
        return true;
      }
    }
    return false;
  }

  @Override
  public void onPause() {
    if (camera != null) camera.release();

    super.onPause();
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    camera.startPreview();
    mPreviewing = true;
  }

  protected Size choosePictureSize(List<Size> localSizes) {
    Size size = null;

    for (Size sz : localSizes) {
      Log.d(LOG, "w: " + sz.width + ", h: " + sz.height);
      if (sz.width > 480 && sz.width <= 640) size = sz;

      if (size != null) break;
    }

    if (size == null) size = localSizes.get(localSizes.size() - 1);
    return size;
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    try {
      camera.setPreviewDisplay(holder);

      Size size = choosePictureSize(camera.getParameters().getSupportedPictureSizes());

      Camera.Parameters params = camera.getParameters();
      params.setPictureSize(size.width, size.height);
      params.setJpegQuality(100);
      params.setJpegThumbnailQuality(100);
      params.setRotation(mRotation); // set rotation to save the picture

      // TODO: set the camera image size that is uniform and small.
      camera.setParameters(params);

    } catch (IOException e) {
      Log.e(LOG, e.toString());
    }
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {}

  @Override
  public void onClick(View view) {
    if (mPreviewing) {
      mPreviewing = false;
      camera.takePicture(null, null, this);
    }
  }

  @Override
  public void onPictureTaken(byte[] data, Camera camera) {

    try {
      String pathToData = IUser.BASE_IMAGE + "_" + baseImages.size();

      if (informaCam.ioService.saveBlob(data, new File(pathToData))) {
        data = null;
        baseImages.add(pathToData);
        progress.setText(String.valueOf(baseImages.size()));

        if (baseImages.size() == 6) {
          button.setClickable(false);
          button.setVisibility(View.GONE);
          progress.setBackgroundResource(R.drawable.progress_accepted);

          JSONArray ja = new JSONArray();
          for (String bi : baseImages) {
            ja.put(bi);
          }

          informaCam.user.put(Models.IUser.PATH_TO_BASE_IMAGE, ja);
          informaCam.user.hasBaseImage = true;
          informaCam.user.save();
          setResult(Activity.RESULT_OK);
          finish();
          return;
        }
      }

      view.post(
          new Runnable() {
            @Override
            public void run() {
              resumePreview();
            }
          });
    } catch (IOException ioe) {
      Log.e(LOG, "error saving picture to iocipher", ioe);
    }
  }

  protected void resumePreview() {
    if (!mPreviewing) {
      camera.startPreview();
      mPreviewing = true;
    }
  }

  public void setCameraDisplayOrientation() {
    if (camera == null || cameraInfo == null) {
      return;
    }

    WindowManager winManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    int rotation = winManager.getDefaultDisplay().getRotation();

    int degrees = 0;

    switch (rotation) {
      case Surface.ROTATION_0:
        degrees = 0;
        break;
      case Surface.ROTATION_90:
        degrees = 90;
        break;
      case Surface.ROTATION_180:
        degrees = 180;
        break;
      case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int mRotation;

    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
      mRotation = (cameraInfo.orientation + degrees) % 360;
      mRotation = (360 - mRotation) % 360; // compensate the mirror
    } else { // back-facing
      mRotation = (cameraInfo.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(mRotation);
  }
}