void resetHistory() {
    mNullFxFilter.onClick(mNullFxFilter);
    mNullBorderFilter.onClick(mNullBorderFilter);

    HistoryAdapter adapter = mImageShow.getHistory();
    adapter.reset();
    ImagePreset original = new ImagePreset(adapter.getItem(0));
    mImageShow.setImagePreset(original);
    mPanelController.resetParameters();
    invalidateViews();
  }
 public void saveImage() {
   if (mImageShow.hasModifications()) {
     // Get the name of the album, to which the image will be saved
     File saveDir = SaveCopyTask.getFinalSaveDirectory(this, mImageLoader.getUri());
     int bucketId = GalleryUtils.getBucketId(saveDir.getPath());
     String albumName = LocalAlbum.getLocalizedName(getResources(), bucketId, null);
     showSavingProgress(albumName);
     mImageShow.saveImage(this, null);
   } else {
     finish();
   }
 }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.filtershow_activity_menu, menu);
    MenuItem showHistory = menu.findItem(R.id.operationsButton);
    if (mShowingHistoryPanel) {
      showHistory.setTitle(R.string.hide_history_panel);
    } else {
      showHistory.setTitle(R.string.show_history_panel);
    }
    MenuItem showState = menu.findItem(R.id.showImageStateButton);
    if (mShowingImageStatePanel) {
      showState.setTitle(R.string.hide_imagestate_panel);
    } else {
      showState.setTitle(R.string.show_imagestate_panel);
    }
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();
    mShareActionProvider.setShareIntent(getDefaultShareIntent());
    mShareActionProvider.setOnShareTargetSelectedListener(this);

    MenuItem undoItem = menu.findItem(R.id.undoButton);
    MenuItem redoItem = menu.findItem(R.id.redoButton);
    MenuItem resetItem = menu.findItem(R.id.resetHistoryButton);
    mImageShow.getHistory().setMenuItems(undoItem, redoItem, resetItem);
    return true;
  }
  public void useImageFilter(
      ImageSmallFilter imageSmallFilter, ImageFilter imageFilter, boolean setBorder) {
    if (imageFilter == null) {
      return;
    }

    if (mCurrentImageSmallFilter != null) {
      mCurrentImageSmallFilter.setSelected(false);
    }
    mCurrentImageSmallFilter = imageSmallFilter;
    mCurrentImageSmallFilter.setSelected(true);

    ImagePreset oldPreset = mImageShow.getImagePreset();
    ImagePreset copy = new ImagePreset(oldPreset);
    // TODO: use a numerical constant instead.

    copy.add(imageFilter);

    mImageShow.setImagePreset(copy);
    invalidateViews();
  }
  @Override
  public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) {
    // First, let's tell the SharedImageProvider that it will need to wait
    // for the image
    Uri uri =
        Uri.withAppendedPath(
            SharedImageProvider.CONTENT_URI, Uri.encode(mSharedOutputFile.getAbsolutePath()));
    ContentValues values = new ContentValues();
    values.put(SharedImageProvider.PREPARE, true);
    getContentResolver().insert(uri, values);
    mSharingImage = true;

    // Process and save the image in the background.
    showSavingProgress(null);
    mImageShow.saveImage(this, mSharedOutputFile);
    return true;
  }
 private int translateMainPanel(View viewPanel) {
   int accessoryPanelWidth = viewPanel.getWidth();
   int mainViewWidth = findViewById(R.id.mainView).getWidth();
   int mainPanelWidth = mImageShow.getDisplayedImageBounds().width();
   if (mainPanelWidth == 0) {
     mainPanelWidth = mainViewWidth;
   }
   int filtersPanelWidth = findViewById(R.id.filtersPanel).getWidth();
   if (mainPanelWidth < filtersPanelWidth) {
     mainPanelWidth = filtersPanelWidth;
   }
   int leftOver = mainViewWidth - mainPanelWidth - accessoryPanelWidth;
   if (leftOver < 0) {
     return -accessoryPanelWidth;
   }
   return 0;
 }
  public void useImagePreset(ImageSmallFilter imageSmallFilter, ImagePreset preset) {
    if (preset == null) {
      return;
    }

    if (mCurrentImageSmallFilter != null) {
      mCurrentImageSmallFilter.setSelected(false);
    }
    mCurrentImageSmallFilter = imageSmallFilter;
    mCurrentImageSmallFilter.setSelected(true);

    ImagePreset copy = new ImagePreset(preset);
    mImageShow.setImagePreset(copy);
    if (preset.isFx()) {
      // if it's an FX we rest the curve adjustment too
      mImageCurves.resetCurve();
    }
    invalidateViews();
  }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.undoButton:
       {
         HistoryAdapter adapter = mImageShow.getHistory();
         int position = adapter.undo();
         mImageShow.onItemClick(position);
         mImageShow.showToast("Undo");
         invalidateViews();
         return true;
       }
     case R.id.redoButton:
       {
         HistoryAdapter adapter = mImageShow.getHistory();
         int position = adapter.redo();
         mImageShow.onItemClick(position);
         mImageShow.showToast("Redo");
         invalidateViews();
         return true;
       }
     case R.id.resetHistoryButton:
       {
         resetHistory();
         return true;
       }
     case R.id.showImageStateButton:
       {
         toggleImageStatePanel();
         return true;
       }
     case R.id.operationsButton:
       {
         toggleHistoryPanel();
         return true;
       }
     case android.R.id.home:
       {
         saveImage();
         return true;
       }
   }
   return false;
 }
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   mImageShow.onItemClick(position);
   invalidateViews();
 }
 public void hideImageViews() {
   mImageShow.setShowControls(false); // reset
   for (View view : mImageViews) {
     view.setVisibility(View.GONE);
   }
 }
 public void invalidateViews() {
   for (ImageShow views : mImageViews) {
     views.invalidate();
     views.updateImage();
   }
 }
 public void showOriginalViews(boolean value) {
   for (ImageShow views : mImageViews) {
     views.showOriginal(value);
   }
 }
  private void fillListImages(LinearLayout listFilters) {
    // TODO: use listview
    // TODO: load the filters straight from the filesystem

    ImageFilterFx[] fxArray = new ImageFilterFx[18];
    int p = 0;

    int[] drawid = {
      R.drawable.filtershow_fx_0005_punch,
      R.drawable.filtershow_fx_0000_vintage,
      R.drawable.filtershow_fx_0004_bw_contrast,
      R.drawable.filtershow_fx_0002_bleach,
      R.drawable.filtershow_fx_0001_instant,
      R.drawable.filtershow_fx_0007_washout,
      R.drawable.filtershow_fx_0003_blue_crush,
      R.drawable.filtershow_fx_0008_washout_color,
      R.drawable.filtershow_fx_0006_x_process
    };

    int[] fxNameid = {
      R.string.ffx_punch,
      R.string.ffx_vintage,
      R.string.ffx_bw_contrast,
      R.string.ffx_bleach,
      R.string.ffx_instant,
      R.string.ffx_washout,
      R.string.ffx_blue_crush,
      R.string.ffx_washout_color,
      R.string.ffx_x_process
    };

    ImagePreset preset = new ImagePreset(getString(R.string.history_original)); // empty
    preset.setImageLoader(mImageLoader);
    mNullFxFilter = new ImageSmallFilter(this);

    mNullFxFilter.setSelected(true);
    mCurrentImageSmallFilter = mNullFxFilter;

    mNullFxFilter.setImageFilter(new ImageFilterFx(null, getString(R.string.none)));

    mNullFxFilter.setController(this);
    mNullFxFilter.setImageLoader(mImageLoader);
    listFilters.addView(mNullFxFilter);
    ImageSmallFilter previousFilter = mNullFxFilter;

    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inScaled = false;

    for (int i = 0; i < drawid.length; i++) {
      Bitmap b = BitmapFactory.decodeResource(getResources(), drawid[i], o);
      fxArray[p++] = new ImageFilterFx(b, getString(fxNameid[i]));
    }
    ImageSmallFilter filter;
    for (int i = 0; i < p; i++) {
      filter = new ImageSmallFilter(this);
      filter.setImageFilter(fxArray[i]);
      filter.setController(this);
      filter.setNulfilter(mNullFxFilter);
      filter.setImageLoader(mImageLoader);
      listFilters.addView(filter);
      previousFilter = filter;
    }

    // Default preset (original)
    mImageShow.setImagePreset(preset);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ImageFilterRS.setRenderScriptContext(this);

    ImageShow.setDefaultBackgroundColor(getResources().getColor(R.color.background_screen));
    ImageSmallFilter.setDefaultBackgroundColor(
        getResources().getColor(R.color.background_main_toolbar));
    // TODO: get those values from XML.
    ImageZoom.setZoomedSize(getPixelsFromDip(256));
    FramedTextButton.setTextSize((int) getPixelsFromDip(14));
    FramedTextButton.setTrianglePadding((int) getPixelsFromDip(4));
    FramedTextButton.setTriangleSize((int) getPixelsFromDip(10));
    ImageShow.setTextSize((int) getPixelsFromDip(12));
    ImageShow.setTextPadding((int) getPixelsFromDip(10));
    ImageShow.setOriginalTextMargin((int) getPixelsFromDip(4));
    ImageShow.setOriginalTextSize((int) getPixelsFromDip(18));
    ImageShow.setOriginalText(getResources().getString(R.string.original_picture_text));
    ImageButtonTitle.setTextSize((int) getPixelsFromDip(12));
    ImageButtonTitle.setTextPadding((int) getPixelsFromDip(10));
    ImageSmallFilter.setMargin((int) getPixelsFromDip(3));
    ImageSmallFilter.setTextMargin((int) getPixelsFromDip(4));
    Drawable curveHandle = getResources().getDrawable(R.drawable.camera_crop);
    int curveHandleSize = (int) getResources().getDimension(R.dimen.crop_indicator_size);
    Spline.setCurveHandle(curveHandle, curveHandleSize);
    Spline.setCurveWidth((int) getPixelsFromDip(3));

    setContentView(R.layout.filtershow_activity);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(R.layout.filtershow_actionbar);

    actionBar
        .getCustomView()
        .setOnClickListener(
            new OnClickListener() {
              @Override
              public void onClick(View view) {
                saveImage();
              }
            });

    mImageLoader = new ImageLoader(this, getApplicationContext());

    LinearLayout listFilters = (LinearLayout) findViewById(R.id.listFilters);
    LinearLayout listBorders = (LinearLayout) findViewById(R.id.listBorders);
    LinearLayout listColors = (LinearLayout) findViewById(R.id.listColorsFx);

    mImageShow = (ImageShow) findViewById(R.id.imageShow);
    mImageCurves = (ImageCurves) findViewById(R.id.imageCurves);
    mImageBorders = (ImageBorder) findViewById(R.id.imageBorder);
    mImageStraighten = (ImageStraighten) findViewById(R.id.imageStraighten);
    mImageZoom = (ImageZoom) findViewById(R.id.imageZoom);
    mImageCrop = (ImageCrop) findViewById(R.id.imageCrop);
    mImageRotate = (ImageRotate) findViewById(R.id.imageRotate);
    mImageFlip = (ImageFlip) findViewById(R.id.imageFlip);
    mImageTinyPlanet = (ImageTinyPlanet) findViewById(R.id.imageTinyPlanet);

    mImageCrop.setAspectTextSize((int) getPixelsFromDip(18));
    ImageCrop.setTouchTolerance((int) getPixelsFromDip(25));
    mImageViews.add(mImageShow);
    mImageViews.add(mImageCurves);
    mImageViews.add(mImageBorders);
    mImageViews.add(mImageStraighten);
    mImageViews.add(mImageZoom);
    mImageViews.add(mImageCrop);
    mImageViews.add(mImageRotate);
    mImageViews.add(mImageFlip);
    mImageViews.add(mImageTinyPlanet);

    mListFx = findViewById(R.id.fxList);
    mListBorders = findViewById(R.id.bordersList);
    mListGeometry = findViewById(R.id.geometryList);
    mListFilterButtons = findViewById(R.id.filterButtonsList);
    mListColors = findViewById(R.id.colorsFxList);
    mListViews.add(mListFx);
    mListViews.add(mListBorders);
    mListViews.add(mListGeometry);
    mListViews.add(mListFilterButtons);
    mListViews.add(mListColors);

    mFxButton = (ImageButton) findViewById(R.id.fxButton);
    mBorderButton = (ImageButton) findViewById(R.id.borderButton);
    mGeometryButton = (ImageButton) findViewById(R.id.geometryButton);
    mColorsButton = (ImageButton) findViewById(R.id.colorsButton);

    mBottomPanelButtons.add(mFxButton);
    mBottomPanelButtons.add(mBorderButton);
    mBottomPanelButtons.add(mGeometryButton);
    mBottomPanelButtons.add(mColorsButton);

    mImageShow.setImageLoader(mImageLoader);
    mImageCurves.setImageLoader(mImageLoader);
    mImageCurves.setMaster(mImageShow);
    mImageBorders.setImageLoader(mImageLoader);
    mImageBorders.setMaster(mImageShow);
    mImageStraighten.setImageLoader(mImageLoader);
    mImageStraighten.setMaster(mImageShow);
    mImageZoom.setImageLoader(mImageLoader);
    mImageZoom.setMaster(mImageShow);
    mImageCrop.setImageLoader(mImageLoader);
    mImageCrop.setMaster(mImageShow);
    mImageRotate.setImageLoader(mImageLoader);
    mImageRotate.setMaster(mImageShow);
    mImageFlip.setImageLoader(mImageLoader);
    mImageFlip.setMaster(mImageShow);
    mImageTinyPlanet.setImageLoader(mImageLoader);
    mImageTinyPlanet.setMaster(mImageShow);

    mPanelController.setActivity(this);

    mPanelController.addImageView(findViewById(R.id.imageShow));
    mPanelController.addImageView(findViewById(R.id.imageCurves));
    mPanelController.addImageView(findViewById(R.id.imageBorder));
    mPanelController.addImageView(findViewById(R.id.imageStraighten));
    mPanelController.addImageView(findViewById(R.id.imageCrop));
    mPanelController.addImageView(findViewById(R.id.imageRotate));
    mPanelController.addImageView(findViewById(R.id.imageFlip));
    mPanelController.addImageView(findViewById(R.id.imageZoom));
    mPanelController.addImageView(findViewById(R.id.imageTinyPlanet));

    mPanelController.addPanel(mFxButton, mListFx, 0);
    mPanelController.addPanel(mBorderButton, mListBorders, 1);

    mPanelController.addPanel(mGeometryButton, mListGeometry, 2);
    mPanelController.addComponent(mGeometryButton, findViewById(R.id.straightenButton));
    mPanelController.addComponent(mGeometryButton, findViewById(R.id.cropButton));
    mPanelController.addComponent(mGeometryButton, findViewById(R.id.rotateButton));
    mPanelController.addComponent(mGeometryButton, findViewById(R.id.flipButton));

    mPanelController.addPanel(mColorsButton, mListColors, 3);

    int[] recastIDs = {
      R.id.tinyplanetButton,
      R.id.vignetteButton,
      R.id.vibranceButton,
      R.id.contrastButton,
      R.id.saturationButton,
      R.id.bwfilterButton,
      R.id.wbalanceButton,
      R.id.hueButton,
      R.id.exposureButton,
      R.id.shadowRecoveryButton
    };
    ImageFilter[] filters = {
      new ImageFilterTinyPlanet(),
      new ImageFilterVignette(),
      new ImageFilterVibrance(),
      new ImageFilterContrast(),
      new ImageFilterSaturated(),
      new ImageFilterBwFilter(),
      new ImageFilterWBalance(),
      new ImageFilterHue(),
      new ImageFilterExposure(),
      new ImageFilterShadows()
    };

    for (int i = 0; i < filters.length; i++) {
      ImageSmallFilter fView = new ImageSmallFilter(this);
      View v = listColors.findViewById(recastIDs[i]);
      int pos = listColors.indexOfChild(v);
      listColors.removeView(v);

      filters[i].setParameter(filters[i].getPreviewParameter());
      if (v instanceof ImageButtonTitle) filters[i].setName(((ImageButtonTitle) v).getText());
      fView.setImageFilter(filters[i]);
      fView.setController(this);
      fView.setImageLoader(mImageLoader);
      fView.setId(recastIDs[i]);
      mPanelController.addComponent(mColorsButton, fView);
      listColors.addView(fView, pos);
    }

    int[] overlayIDs = {R.id.sharpenButton, R.id.curvesButtonRGB};
    int[] overlayBitmaps = {
      R.drawable.filtershow_button_colors_sharpen, R.drawable.filtershow_button_colors_curve
    };
    int[] overlayNames = {R.string.sharpness, R.string.curvesRGB};

    for (int i = 0; i < overlayIDs.length; i++) {
      ImageWithIcon fView = new ImageWithIcon(this);
      View v = listColors.findViewById(overlayIDs[i]);
      int pos = listColors.indexOfChild(v);
      listColors.removeView(v);
      final int sid = overlayNames[i];
      ImageFilterExposure efilter =
          new ImageFilterExposure() {
            {
              mName = getString(sid);
            }
          };
      efilter.setParameter(-300);
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), overlayBitmaps[i]);

      fView.setIcon(bitmap);
      fView.setImageFilter(efilter);
      fView.setController(this);
      fView.setImageLoader(mImageLoader);
      fView.setId(overlayIDs[i]);
      mPanelController.addComponent(mColorsButton, fView);
      listColors.addView(fView, pos);
    }

    mPanelController.addView(findViewById(R.id.applyEffect));
    mPanelController.addView(findViewById(R.id.pickCurvesChannel));
    mPanelController.addView(findViewById(R.id.aspect));
    findViewById(R.id.resetOperationsButton)
        .setOnClickListener(createOnClickResetOperationsButton());

    ListView operationsList = (ListView) findViewById(R.id.operationsList);
    operationsList.setAdapter(mImageShow.getHistory());
    operationsList.setOnItemClickListener(this);
    ListView imageStateList = (ListView) findViewById(R.id.imageStateList);
    imageStateList.setAdapter(mImageShow.getImageStateAdapter());
    mImageLoader.setAdapter(mImageShow.getHistory());

    fillListImages(listFilters);
    fillListBorders(listBorders);

    SeekBar seekBar = (SeekBar) findViewById(R.id.filterSeekBar);
    seekBar.setMax(SEEK_BAR_MAX);

    mImageShow.setSeekBar(seekBar);
    mImageZoom.setSeekBar(seekBar);
    mImageTinyPlanet.setSeekBar(seekBar);
    mPanelController.setRowPanel(findViewById(R.id.secondRowPanel));
    mPanelController.setUtilityPanel(
        this,
        findViewById(R.id.filterButtonsList),
        findViewById(R.id.applyEffect),
        findViewById(R.id.aspect),
        findViewById(R.id.pickCurvesChannel));
    mPanelController.setMasterImage(mImageShow);
    mPanelController.setCurrentPanel(mFxButton);
    Intent intent = getIntent();
    if (intent.getBooleanExtra(LAUNCH_FULLSCREEN, false)) {
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    if (intent.getData() != null) {
      startLoadBitmap(intent.getData());
    } else {
      pickImage();
    }

    String action = intent.getAction();
    if (action.equalsIgnoreCase(CROP_ACTION)) {
      mPanelController.showComponent(findViewById(R.id.cropButton));
    } else if (action.equalsIgnoreCase(TINY_PLANET_ACTION)) {
      mPanelController.showComponent(findViewById(R.id.tinyplanetButton));
    }
  }