Android Open Source - HockeySDK-Android Paint Activity






From Project

Back to project page HockeySDK-Android.

License

The source code is released under:

Apache License

If you think the Android project HockeySDK-Android 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 net.hockeyapp.android;
/* w w w . j a  v a2 s . c o  m*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;
import net.hockeyapp.android.views.PaintView;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author Patrick Eschenbach
 */
public class PaintActivity extends Activity {

  private static final int MENU_SAVE_ID  = Menu.FIRST;
  private static final int MENU_UNDO_ID  = Menu.FIRST + 1;
  private static final int MENU_CLEAR_ID = Menu.FIRST + 2;

  private PaintView paintView;
  private String imageName;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Get image path. */
    Bundle extras = getIntent().getExtras();
    Uri imageUri = extras.getParcelable("imageUri");

    imageName = determineFilename(imageUri, imageUri.getLastPathSegment());

    int displayWidth = getResources().getDisplayMetrics().widthPixels;
    int displayHeight = getResources().getDisplayMetrics().heightPixels;
    int currentOrientation = displayWidth > displayHeight ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
                                                            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

    int desiredOrientation = PaintView.determineOrientation(getContentResolver(), imageUri);
    setRequestedOrientation(desiredOrientation);

    if (currentOrientation != desiredOrientation) {
      /* Activity will be destroyed again.. skip the following expensive operations. */
      Log.d(Constants.TAG, "Image loading skipped because activity will be destroyed for orientation change.");
      return;
    }

    /* Create view and find out which orientation is needed. */
    paintView = new PaintView(this, imageUri, displayWidth, displayHeight);

    LinearLayout vLayout = new LinearLayout(this);
    LinearLayout.LayoutParams vParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    vLayout.setLayoutParams(vParams);
    vLayout.setGravity(Gravity.CENTER);
    vLayout.setOrientation(LinearLayout.VERTICAL);

    LinearLayout hLayout = new LinearLayout(this);
    LinearLayout.LayoutParams hParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    hLayout.setLayoutParams(hParams);
    hLayout.setGravity(Gravity.CENTER);
    hLayout.setOrientation(LinearLayout.HORIZONTAL);

    vLayout.addView(hLayout);
    hLayout.addView(paintView);
    setContentView(vLayout);

    Toast toast = Toast.makeText(this, Strings.get(Strings.PAINT_INDICATOR_TOAST_ID), 1000);
    toast.show();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menu.add(0, MENU_SAVE_ID,  0, Strings.get(Strings.PAINT_MENU_SAVE_ID));
    menu.add(0, MENU_UNDO_ID,  0, Strings.get(Strings.PAINT_MENU_UNDO_ID));
    menu.add(0, MENU_CLEAR_ID, 0, Strings.get(Strings.PAINT_MENU_CLEAR_ID));

    return true;
  }

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case MENU_SAVE_ID:
        makeResult();
        return true;

      case MENU_UNDO_ID:
        paintView.undo();
        return true;

      case MENU_CLEAR_ID:
        paintView.clearImage();
        return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      if (!paintView.isClear()) {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            switch (which){
              case DialogInterface.BUTTON_POSITIVE:
                PaintActivity.this.finish();
                break;

              case DialogInterface.BUTTON_NEGATIVE:
                /* No action. */
                break;
            }
          }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(Strings.get(Strings.PAINT_DIALOG_MESSAGE_ID))
               .setPositiveButton(Strings.get(Strings.PAINT_DIALOG_POSITIVE_BUTTON_ID), dialogClickListener)
               .setNegativeButton(Strings.get(Strings.PAINT_DIALOG_NEGATIVE_BUTTON_ID), dialogClickListener)
               .show();
        return true;
      }
    }

    return super.onKeyDown(keyCode, event);
  }

  private void makeResult() {
    File hockeyAppCache = new File(getCacheDir(), Constants.TAG);
    hockeyAppCache.mkdir();
    File result = new File(hockeyAppCache, imageName);

    int suffix = 1;
    while (result.exists()) {
      result = new File(hockeyAppCache, imageName + "_" + suffix);
      suffix++;
    }

    paintView.setDrawingCacheEnabled(true);
    final Bitmap bitmap = paintView.getDrawingCache();
    new AsyncTask<File, Void, Void>() {
      @Override
      protected Void doInBackground(File... args) {
        try {
          FileOutputStream out = new FileOutputStream(args[0]);
          bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
          out.close();
        }
        catch (Exception e) {
          e.printStackTrace();
          Log.e(Constants.TAG, "Could not save image.", e);
        }
        return null;
      }
    }.execute(result);

    Intent intent = new Intent();
    Uri uri = Uri.fromFile(result);
    intent.putExtra("imageUri", uri);

    if (getParent() == null) {
      setResult(Activity.RESULT_OK, intent);
    }
    else {
      getParent().setResult(Activity.RESULT_OK, intent);
    }
    finish();
  }

  private String determineFilename(Uri uri, String fallback) {
    String[] projection = {MediaStore.MediaColumns.DATA};
    String path = null;

    ContentResolver cr = getApplicationContext().getContentResolver();
    Cursor metaCursor = cr.query(uri, projection, null, null, null);

    if (metaCursor != null) {
      try {
        if (metaCursor.moveToFirst()) {
          path = metaCursor.getString(0);
        }
      }
      finally {
        metaCursor.close();
      }
    }
    return path == null ? fallback : new File(path).getName();
  }
}




Java Source Code List

net.hockeyapp.android.Constants.java
net.hockeyapp.android.CrashManagerListener.java
net.hockeyapp.android.CrashManager.java
net.hockeyapp.android.ExceptionHandler.java
net.hockeyapp.android.ExpiryInfoActivity.java
net.hockeyapp.android.FeedbackActivityInterface.java
net.hockeyapp.android.FeedbackActivity.java
net.hockeyapp.android.FeedbackManagerListener.java
net.hockeyapp.android.FeedbackManager.java
net.hockeyapp.android.LocaleManager.java
net.hockeyapp.android.LoginActivity.java
net.hockeyapp.android.LoginManagerListener.java
net.hockeyapp.android.LoginManager.java
net.hockeyapp.android.PaintActivity.java
net.hockeyapp.android.StringListener.java
net.hockeyapp.android.Strings.java
net.hockeyapp.android.Tracking.java
net.hockeyapp.android.UpdateActivityInterface.java
net.hockeyapp.android.UpdateActivity.java
net.hockeyapp.android.UpdateFragment.java
net.hockeyapp.android.UpdateInfoListener.java
net.hockeyapp.android.UpdateManagerListener.java
net.hockeyapp.android.UpdateManager.java
net.hockeyapp.android.adapters.MessagesAdapter.java
net.hockeyapp.android.listeners.DownloadFileListener.java
net.hockeyapp.android.listeners.SendFeedbackListener.java
net.hockeyapp.android.objects.ErrorObject.java
net.hockeyapp.android.objects.FeedbackAttachment.java
net.hockeyapp.android.objects.FeedbackMessage.java
net.hockeyapp.android.objects.FeedbackResponse.java
net.hockeyapp.android.objects.Feedback.java
net.hockeyapp.android.tasks.AttachmentDownloader.java
net.hockeyapp.android.tasks.CheckUpdateTaskWithUI.java
net.hockeyapp.android.tasks.CheckUpdateTask.java
net.hockeyapp.android.tasks.DownloadFileTask.java
net.hockeyapp.android.tasks.GetFileSizeTask.java
net.hockeyapp.android.tasks.LoginTask.java
net.hockeyapp.android.tasks.ParseFeedbackTask.java
net.hockeyapp.android.tasks.SendFeedbackTask.java
net.hockeyapp.android.utils.AsyncTaskUtils.java
net.hockeyapp.android.utils.Base64.java
net.hockeyapp.android.utils.ConnectionManager.java
net.hockeyapp.android.utils.DeviceUtils.java
net.hockeyapp.android.utils.FeedbackParser.java
net.hockeyapp.android.utils.ImageUtils.java
net.hockeyapp.android.utils.PrefsUtil.java
net.hockeyapp.android.utils.SimpleMultipartEntity.java
net.hockeyapp.android.utils.UiThreadUtil.java
net.hockeyapp.android.utils.Util.java
net.hockeyapp.android.utils.VersionCache.java
net.hockeyapp.android.utils.VersionHelper.java
net.hockeyapp.android.utils.ViewHelper.java
net.hockeyapp.android.views.AttachmentListView.java
net.hockeyapp.android.views.AttachmentView.java
net.hockeyapp.android.views.ExpiryInfoView.java
net.hockeyapp.android.views.FeedbackMessageView.java
net.hockeyapp.android.views.FeedbackView.java
net.hockeyapp.android.views.LoginView.java
net.hockeyapp.android.views.PaintView.java
net.hockeyapp.android.views.UpdateView.java