FilterShowActivity.java revision fb6a8e166f0a46d2a994a408b1679e3a67fdcb52
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.filtershow;
18
19import android.app.ActionBar;
20import android.app.AlertDialog;
21import android.app.ProgressDialog;
22import android.app.WallpaperManager;
23import android.content.ContentValues;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.content.Intent;
27import android.content.res.Configuration;
28import android.content.res.Resources;
29import android.graphics.Bitmap;
30import android.graphics.Color;
31import android.graphics.Point;
32import android.graphics.drawable.Drawable;
33import android.net.Uri;
34import android.os.AsyncTask;
35import android.os.Bundle;
36import android.provider.MediaStore;
37import android.support.v4.app.FragmentActivity;
38import android.support.v4.app.FragmentTransaction;
39import android.util.DisplayMetrics;
40import android.util.Log;
41import android.util.TypedValue;
42import android.view.*;
43import android.view.View.OnClickListener;
44import android.widget.AdapterView;
45import android.widget.AdapterView.OnItemClickListener;
46import android.widget.FrameLayout;
47import android.widget.ImageButton;
48import android.widget.LinearLayout;
49import android.widget.ListView;
50import android.widget.ShareActionProvider;
51import android.widget.ShareActionProvider.OnShareTargetSelectedListener;
52import android.widget.Toast;
53
54import com.android.gallery3d.R;
55import com.android.gallery3d.data.LocalAlbum;
56import com.android.gallery3d.filtershow.cache.CachingPipeline;
57import com.android.gallery3d.filtershow.cache.FilteringPipeline;
58import com.android.gallery3d.filtershow.cache.ImageLoader;
59import com.android.gallery3d.filtershow.crop.CropExtras;
60import com.android.gallery3d.filtershow.editors.BasicEditor;
61import com.android.gallery3d.filtershow.editors.EditorCrop;
62import com.android.gallery3d.filtershow.editors.EditorDraw;
63import com.android.gallery3d.filtershow.editors.EditorFlip;
64import com.android.gallery3d.filtershow.editors.EditorInfo;
65import com.android.gallery3d.filtershow.editors.EditorManager;
66import com.android.gallery3d.filtershow.editors.EditorRedEye;
67import com.android.gallery3d.filtershow.editors.EditorRotate;
68import com.android.gallery3d.filtershow.editors.EditorStraighten;
69import com.android.gallery3d.filtershow.editors.EditorTinyPlanet;
70import com.android.gallery3d.filtershow.editors.ImageOnlyEditor;
71import com.android.gallery3d.filtershow.filters.*;
72import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
73import com.android.gallery3d.filtershow.imageshow.ImageCrop;
74import com.android.gallery3d.filtershow.imageshow.ImageShow;
75import com.android.gallery3d.filtershow.imageshow.ImageTinyPlanet;
76import com.android.gallery3d.filtershow.imageshow.MasterImage;
77import com.android.gallery3d.filtershow.presets.ImagePreset;
78import com.android.gallery3d.filtershow.provider.SharedImageProvider;
79import com.android.gallery3d.filtershow.state.StateAdapter;
80import com.android.gallery3d.filtershow.state.StatePanel;
81import com.android.gallery3d.filtershow.tools.BitmapTask;
82import com.android.gallery3d.filtershow.tools.SaveCopyTask;
83import com.android.gallery3d.filtershow.ui.FilterIconButton;
84import com.android.gallery3d.filtershow.ui.FramedTextButton;
85import com.android.gallery3d.filtershow.ui.Spline;
86import com.android.gallery3d.util.GalleryUtils;
87import com.android.photos.data.GalleryBitmapPool;
88
89import java.io.File;
90import java.io.IOException;
91import java.lang.ref.WeakReference;
92import java.util.Vector;
93
94public class FilterShowActivity extends FragmentActivity implements OnItemClickListener,
95        OnShareTargetSelectedListener {
96
97    private String mPanelFragmentTag = "StatePanel";
98
99    // fields for supporting crop action
100    public static final String CROP_ACTION = "com.android.camera.action.CROP";
101    private CropExtras mCropExtras = null;
102    private String mAction = "";
103    MasterImage mMasterImage = null;
104
105    private static final long LIMIT_SUPPORTS_HIGHRES = 134217728; // 128Mb
106
107    public static final String TINY_PLANET_ACTION = "com.android.camera.action.TINY_PLANET";
108    public static final String LAUNCH_FULLSCREEN = "launch-fullscreen";
109    public static final int MAX_BMAP_IN_INTENT = 990000;
110    private final PanelController mPanelController = new PanelController();
111    private ImageLoader mImageLoader = null;
112    private ImageShow mImageShow = null;
113    private ImageTinyPlanet mImageTinyPlanet = null;
114
115    private View mSaveButton = null;
116
117    private EditorPlaceHolder mEditorPlaceHolder = new EditorPlaceHolder(this);
118
119    private static final int SELECT_PICTURE = 1;
120    private static final String LOGTAG = "FilterShowActivity";
121    protected static final boolean ANIMATE_PANELS = true;
122    private static int mImageBorderSize = 4; // in percent
123
124    private boolean mShowingTinyPlanet = false;
125    private boolean mShowingHistoryPanel = false;
126    private boolean mShowingImageStatePanel = false;
127
128    private final Vector<ImageShow> mImageViews = new Vector<ImageShow>();
129
130    private ShareActionProvider mShareActionProvider;
131    private File mSharedOutputFile = null;
132
133    private boolean mSharingImage = false;
134
135    private WeakReference<ProgressDialog> mSavingProgressDialog;
136
137    private LoadBitmapTask mLoadBitmapTask;
138    private FilterIconButton mNullFxFilter;
139    private FilterIconButton mNullBorderFilter;
140    private int mIconSeedSize = 140;
141
142    private View mImageCategoryPanel = null;
143
144    @Override
145    public void onCreate(Bundle savedInstanceState) {
146        super.onCreate(savedInstanceState);
147
148        clearGalleryBitmapPool();
149
150        CachingPipeline.createRenderscriptContext(this);
151        setupMasterImage();
152        setDefaultValues();
153        fillEditors();
154
155        loadXML();
156        if (getResources().getConfiguration().orientation
157                == Configuration.ORIENTATION_LANDSCAPE) {
158            mShowingImageStatePanel = true;
159        }
160        if (mShowingHistoryPanel) {
161            findViewById(R.id.historyPanel).setVisibility(View.VISIBLE);
162        } else {
163            findViewById(R.id.historyPanel).setVisibility(View.GONE);
164        }
165        if (mShowingImageStatePanel && (savedInstanceState == null)) {
166            loadImageStatePanel();
167        }
168
169        setDefaultPreset();
170
171        processIntent();
172    }
173
174    private void loadXML() {
175        setContentView(R.layout.filtershow_activity);
176
177        ((ViewStub) findViewById(R.id.stateCategoryStub)).inflate();
178        ((ViewStub) findViewById(R.id.editorPanelStub)).inflate();
179        ((ViewStub) findViewById(R.id.historyPanelStub)).inflate();
180
181        ActionBar actionBar = getActionBar();
182        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
183        actionBar.setCustomView(R.layout.filtershow_actionbar);
184
185        mSaveButton = actionBar.getCustomView();
186        mSaveButton.setOnClickListener(new OnClickListener() {
187            @Override
188            public void onClick(View view) {
189                saveImage();
190            }
191        });
192
193        mImageShow = (ImageShow) findViewById(R.id.imageShow);
194        mImageTinyPlanet = (ImageTinyPlanet) findViewById(R.id.imageTinyPlanet);
195        mImageViews.add(mImageShow);
196        mImageViews.add(mImageTinyPlanet);
197
198        setupEditors();
199
200        mEditorPlaceHolder.hide();
201
202        mImageShow.setImageLoader(mImageLoader);
203        mImageTinyPlanet.setImageLoader(mImageLoader);
204
205        mPanelController.clear();
206        mPanelController.setActivity(this);
207        mPanelController.setEditorPlaceHolder(mEditorPlaceHolder);
208
209        mPanelController.addImageView(findViewById(R.id.imageShow));
210        mPanelController.addImageView(findViewById(R.id.imageTinyPlanet));
211
212        mPanelController.addPanel(R.id.fxButton, R.id.fxList, 0);
213        mPanelController.addPanel(R.id.borderButton, R.id.bordersList, 1);
214        mPanelController.addPanel(R.id.geometryButton, R.id.geometryList, 2);
215        mPanelController.addPanel(R.id.colorsButton, R.id.colorsFxList, 3);
216
217        fillFx((LinearLayout) findViewById(R.id.listFilters), R.id.fxButton);
218        setupBorders();
219        fillGeometry();
220        fillFilters();
221
222        mPanelController.addView(findViewById(R.id.applyEffect));
223
224        setupHistoryPanel();
225        setupStatePanel();
226
227        mImageCategoryPanel = findViewById(R.id.imageCategoryPanel);
228    }
229
230    public void hideCategoryPanel() {
231        mImageCategoryPanel.setVisibility(View.GONE);
232    }
233
234    public void showCategoryPanel() {
235        mImageCategoryPanel.setVisibility(View.VISIBLE);
236    }
237
238    public void setupHistoryPanel() {
239        findViewById(R.id.resetOperationsButton).setOnClickListener(
240                createOnClickResetOperationsButton());
241        ListView operationsList = (ListView) findViewById(R.id.operationsList);
242        operationsList.setAdapter(mMasterImage.getHistory());
243        operationsList.setOnItemClickListener(this);
244    }
245
246    public void setupStatePanel() {
247        mImageLoader.setAdapter(mMasterImage.getHistory());
248        mPanelController.setRowPanel(findViewById(R.id.secondRowPanel));
249        mPanelController.setUtilityPanel(this, findViewById(R.id.filterButtonsList));
250        mPanelController.setCurrentPanel(R.id.fxButton);
251    }
252
253    private void fillPanel(Vector<FilterRepresentation> representations, int layoutId, int buttonId) {
254        ImageButton button = (ImageButton) findViewById(buttonId);
255        LinearLayout layout = (LinearLayout) findViewById(layoutId);
256
257        for (FilterRepresentation representation : representations) {
258            setupFilterRepresentationButton(representation, layout, button);
259        }
260    }
261
262    private void fillFilters() {
263        Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>();
264        FiltersManager filtersManager = FiltersManager.getManager();
265        filtersManager.addEffects(filtersRepresentations);
266        fillPanel(filtersRepresentations, R.id.listColorsFx, R.id.colorsButton);
267    }
268
269    private void fillGeometry() {
270        Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>();
271        FiltersManager filtersManager = FiltersManager.getManager();
272
273        GeometryMetadata geo = new GeometryMetadata();
274        int[] editorsId = geo.getEditorIds();
275        for (int i = 0; i < editorsId.length; i++) {
276            int editorId = editorsId[i];
277            GeometryMetadata geometry = new GeometryMetadata(geo);
278            geometry.setEditorId(editorId);
279            EditorInfo editorInfo = (EditorInfo) mEditorPlaceHolder.getEditor(editorId);
280            geometry.setTextId(editorInfo.getTextId());
281            geometry.setOverlayId(editorInfo.getOverlayId());
282            geometry.setOverlayOnly(editorInfo.getOverlayOnly());
283            filtersRepresentations.add(geometry);
284        }
285
286        filtersManager.addTools(filtersRepresentations);
287        fillPanel(filtersRepresentations, R.id.listGeometry, R.id.geometryButton);
288    }
289
290    private void processIntent() {
291        Intent intent = getIntent();
292        if (intent.getBooleanExtra(LAUNCH_FULLSCREEN, false)) {
293            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
294        }
295
296        mAction = intent.getAction();
297
298        if (intent.getData() != null) {
299            startLoadBitmap(intent.getData());
300        } else {
301            pickImage();
302        }
303
304        // Handle behavior for various actions
305        if (mAction.equalsIgnoreCase(CROP_ACTION)) {
306            Bundle extras = intent.getExtras();
307            if (extras != null) {
308                mCropExtras = new CropExtras(extras.getInt(CropExtras.KEY_OUTPUT_X, 0),
309                        extras.getInt(CropExtras.KEY_OUTPUT_Y, 0),
310                        extras.getBoolean(CropExtras.KEY_SCALE, true) &&
311                                extras.getBoolean(CropExtras.KEY_SCALE_UP_IF_NEEDED, false),
312                        extras.getInt(CropExtras.KEY_ASPECT_X, 0),
313                        extras.getInt(CropExtras.KEY_ASPECT_Y, 0),
314                        extras.getBoolean(CropExtras.KEY_SET_AS_WALLPAPER, false),
315                        extras.getBoolean(CropExtras.KEY_RETURN_DATA, false),
316                        (Uri) extras.getParcelable(MediaStore.EXTRA_OUTPUT),
317                        extras.getString(CropExtras.KEY_OUTPUT_FORMAT),
318                        extras.getBoolean(CropExtras.KEY_SHOW_WHEN_LOCKED, false),
319                        extras.getFloat(CropExtras.KEY_SPOTLIGHT_X),
320                        extras.getFloat(CropExtras.KEY_SPOTLIGHT_Y));
321
322                if (mCropExtras.getShowWhenLocked()) {
323                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
324                }
325                mImageShow.getImagePreset().mGeoData.setCropExtras(mCropExtras);
326
327                // FIXME: moving to editors breaks the crop action
328                EditorCrop crop = (EditorCrop) mEditorPlaceHolder.getEditor(EditorCrop.ID);
329
330                crop.setExtras(mCropExtras);
331                String s = getString(R.string.Fixed);
332                crop.setAspectString(s);
333                crop.setCropActionFlag(true);
334                mPanelController.setFixedAspect(mCropExtras.getAspectX() > 0
335                        && mCropExtras.getAspectY() > 0);
336            }
337        }
338    }
339
340    private void setupEditors() {
341        mEditorPlaceHolder.setContainer((FrameLayout) findViewById(R.id.editorContainer));
342        EditorManager.addEditors(mEditorPlaceHolder);
343        mEditorPlaceHolder.setOldViews(mImageViews);
344        mEditorPlaceHolder.setImageLoader(mImageLoader);
345    }
346
347    private void fillEditors() {
348        mEditorPlaceHolder.addEditor(new EditorDraw());
349        mEditorPlaceHolder.addEditor(new BasicEditor());
350        mEditorPlaceHolder.addEditor(new ImageOnlyEditor());
351        mEditorPlaceHolder.addEditor(new EditorTinyPlanet());
352        mEditorPlaceHolder.addEditor(new EditorRedEye());
353        mEditorPlaceHolder.addEditor(new EditorCrop());
354        mEditorPlaceHolder.addEditor(new EditorFlip());
355        mEditorPlaceHolder.addEditor(new EditorRotate());
356        mEditorPlaceHolder.addEditor(new EditorStraighten());
357    }
358
359    private void setDefaultValues() {
360        ImageFilter.setActivityForMemoryToasts(this);
361
362        Resources res = getResources();
363        FiltersManager.setResources(res);
364
365        ImageShow.setDefaultBackgroundColor(res.getColor(R.color.background_screen));
366        // TODO: get those values from XML.
367        FramedTextButton.setTextSize((int) getPixelsFromDip(14));
368        FramedTextButton.setTrianglePadding((int) getPixelsFromDip(4));
369        FramedTextButton.setTriangleSize((int) getPixelsFromDip(10));
370        ImageShow.setTextSize((int) getPixelsFromDip(12));
371        ImageShow.setTextPadding((int) getPixelsFromDip(10));
372        ImageShow.setOriginalTextMargin((int) getPixelsFromDip(4));
373        ImageShow.setOriginalTextSize((int) getPixelsFromDip(18));
374        ImageShow.setOriginalText(res.getString(R.string.original_picture_text));
375        mIconSeedSize = res.getDimensionPixelSize(R.dimen.thumbnail_size);
376        // TODO: pick correct value
377        // MasterImage.setIconSeedSize(mIconSeedSize);
378
379        Drawable curveHandle = res.getDrawable(R.drawable.camera_crop);
380        int curveHandleSize = (int) res.getDimension(R.dimen.crop_indicator_size);
381        Spline.setCurveHandle(curveHandle, curveHandleSize);
382        Spline.setCurveWidth((int) getPixelsFromDip(3));
383
384        ImageCrop.setAspectTextSize((int) getPixelsFromDip(18));
385        ImageCrop.setTouchTolerance((int) getPixelsFromDip(25));
386        ImageCrop.setMinCropSize((int) getPixelsFromDip(55));
387    }
388
389    private void startLoadBitmap(Uri uri) {
390        final View filters = findViewById(R.id.filtersPanel);
391        final View loading = findViewById(R.id.loading);
392        final View imageShow = findViewById(R.id.imageShow);
393        imageShow.setVisibility(View.INVISIBLE);
394        filters.setVisibility(View.INVISIBLE);
395        loading.setVisibility(View.VISIBLE);
396
397        View tinyPlanetView = findViewById(EditorTinyPlanet.ID);
398        if (tinyPlanetView != null) {
399            mShowingTinyPlanet = false;
400            tinyPlanetView.setVisibility(View.GONE);
401        }
402        mLoadBitmapTask = new LoadBitmapTask(tinyPlanetView);
403        mLoadBitmapTask.execute(uri);
404    }
405
406    private void setupBorders() {
407        LinearLayout list = (LinearLayout) findViewById(R.id.listBorders);
408        Vector<FilterRepresentation> borders = new Vector<FilterRepresentation>();
409        ImageButton borderButton = (ImageButton) findViewById(R.id.borderButton);
410
411        // The "no border" implementation
412        borders.add(new FilterImageBorderRepresentation(0));
413
414        // Google-build borders
415        FiltersManager.getManager().addBorders(borders);
416
417        // Regular borders
418        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_4x5));
419        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_brush));
420        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_grunge));
421        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_sumi_e));
422        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_tape));
423        borders.add(new FilterColorBorderRepresentation(Color.BLACK, mImageBorderSize, 0));
424        borders.add(new FilterColorBorderRepresentation(Color.BLACK, mImageBorderSize,
425                mImageBorderSize));
426        borders.add(new FilterColorBorderRepresentation(Color.WHITE, mImageBorderSize, 0));
427        borders.add(new FilterColorBorderRepresentation(Color.WHITE, mImageBorderSize,
428                mImageBorderSize));
429        int creamColor = Color.argb(255, 237, 237, 227);
430        borders.add(new FilterColorBorderRepresentation(creamColor, mImageBorderSize, 0));
431        borders.add(new FilterColorBorderRepresentation(creamColor, mImageBorderSize,
432                mImageBorderSize));
433        for (int i = 0; i < borders.size(); i++) {
434            FilterRepresentation filter = borders.elementAt(i);
435            filter.setName(getString(R.string.borders));
436            if (i == 0) {
437                filter.setName(getString(R.string.none));
438            }
439            FilterIconButton b = setupFilterRepresentationButton(filter, list, borderButton);
440            if (i == 0) {
441                mNullBorderFilter = b;
442                mNullBorderFilter.setSelected(true);
443            }
444        }
445    }
446
447    private class LoadBitmapTask extends AsyncTask<Uri, Boolean, Boolean> {
448        View mTinyPlanetButton;
449        int mBitmapSize;
450
451        public LoadBitmapTask(View button) {
452            mTinyPlanetButton = button;
453            mBitmapSize = getScreenImageSize();
454        }
455
456        @Override
457        protected Boolean doInBackground(Uri... params) {
458            if (!mImageLoader.loadBitmap(params[0], mBitmapSize)) {
459                return false;
460            }
461            publishProgress(mImageLoader.queryLightCycle360());
462            return true;
463        }
464
465        @Override
466        protected void onProgressUpdate(Boolean... values) {
467            super.onProgressUpdate(values);
468            if (isCancelled()) {
469                return;
470            }
471            if (values[0]) {
472                mShowingTinyPlanet = true;
473                mTinyPlanetButton.setVisibility(View.VISIBLE);
474            }
475        }
476
477        @Override
478        protected void onPostExecute(Boolean result) {
479
480            if (isCancelled()) {
481                return;
482            }
483
484            if (!result) {
485                cannotLoadImage();
486            }
487
488            final View loading = findViewById(R.id.loading);
489            loading.setVisibility(View.GONE);
490            final View filters = findViewById(R.id.filtersPanel);
491            filters.setVisibility(View.VISIBLE);
492            if (PanelController.useAnimationsLayer()) {
493                float y = filters.getY();
494                filters.setY(y + filters.getHeight());
495                filters.animate().setDuration(600).y(y).withLayer().start();
496            }
497            final View imageShow = findViewById(R.id.imageShow);
498            imageShow.setVisibility(View.VISIBLE);
499
500            Bitmap largeBitmap = mImageLoader.getOriginalBitmapLarge();
501            FilteringPipeline pipeline = FilteringPipeline.getPipeline();
502            pipeline.setOriginal(largeBitmap);
503            float previewScale = (float) largeBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width();
504            pipeline.setPreviewScaleFactor(previewScale);
505            Bitmap highresBitmap = mImageLoader.getOriginalBitmapHighres();
506            if (highresBitmap != null) {
507                float highResPreviewScale = (float) highresBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width();
508                pipeline.setHighResPreviewScaleFactor(highResPreviewScale);
509            }
510            pipeline.turnOnPipeline(true);
511            MasterImage.getImage().setOriginalGeometry(largeBitmap);
512            MasterImage.getImage().getHistory().setOriginalBitmap(mImageLoader.getOriginalBitmapSmall());
513            mLoadBitmapTask = null;
514
515            if (mAction == CROP_ACTION) {
516                mPanelController.showComponent(findViewById(EditorCrop.ID));
517            } else if (mAction == TINY_PLANET_ACTION) {
518                mPanelController.showComponent(findViewById(EditorTinyPlanet.ID));
519            }
520            super.onPostExecute(result);
521        }
522
523    }
524
525    private void clearGalleryBitmapPool() {
526        (new AsyncTask<Void, Void, Void>() {
527            @Override
528            protected Void doInBackground(Void... params) {
529                // Free memory held in Gallery's Bitmap pool.  May be O(n) for n bitmaps.
530                GalleryBitmapPool.getInstance().clear();
531                return null;
532            }
533        }).execute();
534    }
535
536    @Override
537    protected void onDestroy() {
538        if (mLoadBitmapTask != null) {
539            mLoadBitmapTask.cancel(false);
540        }
541        // TODO:  Using singletons is a bad design choice for many of these
542        // due static reference leaks and in general.  Please refactor.
543        FilteringPipeline.getPipeline().turnOnPipeline(false);
544        MasterImage.reset();
545        FilteringPipeline.reset();
546        ImageFilter.resetStatics();
547        FiltersManager.getPreviewManager().freeRSFilterScripts();
548        FiltersManager.getManager().freeRSFilterScripts();
549        FiltersManager.getHighresManager().freeRSFilterScripts();
550        FiltersManager.reset();
551        CachingPipeline.destroyRenderScriptContext();
552        super.onDestroy();
553    }
554
555    private int translateMainPanel(View viewPanel) {
556        int accessoryPanelWidth = viewPanel.getWidth();
557        if (accessoryPanelWidth == 0) {
558            // TODO: fixes this by using a fragment. Currently,
559            // the first time we get called the panel hasn't been
560            // layed out yet, so we get a size zero.
561            accessoryPanelWidth = (int) getPixelsFromDip(200);
562        }
563        int mainViewWidth = findViewById(R.id.mainView).getWidth();
564        int mainPanelWidth = mImageShow.getDisplayedImageBounds().width();
565        if (mainPanelWidth == 0) {
566            mainPanelWidth = mainViewWidth;
567        }
568        int filtersPanelWidth = findViewById(R.id.filtersPanel).getWidth();
569        if (mainPanelWidth < filtersPanelWidth) {
570            mainPanelWidth = filtersPanelWidth;
571        }
572        int leftOver = mainViewWidth - mainPanelWidth - accessoryPanelWidth;
573        if (leftOver < 0) {
574            return -accessoryPanelWidth;
575        }
576        return 0;
577    }
578
579    private int getScreenImageSize() {
580        DisplayMetrics metrics = new DisplayMetrics();
581        Display display = getWindowManager().getDefaultDisplay();
582        Point size = new Point();
583        display.getSize(size);
584        display.getMetrics(metrics);
585        int msize = Math.min(size.x, size.y);
586        return (133 * msize) / metrics.densityDpi;
587    }
588
589    private void showSavingProgress(String albumName) {
590        ProgressDialog progress;
591        if (mSavingProgressDialog != null) {
592            progress = mSavingProgressDialog.get();
593            if (progress != null) {
594                progress.show();
595                return;
596            }
597        }
598        // TODO: Allow cancellation of the saving process
599        String progressText;
600        if (albumName == null) {
601            progressText = getString(R.string.saving_image);
602        } else {
603            progressText = getString(R.string.filtershow_saving_image, albumName);
604        }
605        progress = ProgressDialog.show(this, "", progressText, true, false);
606        mSavingProgressDialog = new WeakReference<ProgressDialog>(progress);
607    }
608
609    private void hideSavingProgress() {
610        if (mSavingProgressDialog != null) {
611            ProgressDialog progress = mSavingProgressDialog.get();
612            if (progress != null)
613                progress.dismiss();
614        }
615    }
616
617    public void completeSaveImage(Uri saveUri) {
618        if (mSharingImage && mSharedOutputFile != null) {
619            // Image saved, we unblock the content provider
620            Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI,
621                    Uri.encode(mSharedOutputFile.getAbsolutePath()));
622            ContentValues values = new ContentValues();
623            values.put(SharedImageProvider.PREPARE, false);
624            getContentResolver().insert(uri, values);
625        }
626        setResult(RESULT_OK, new Intent().setData(saveUri));
627        hideSavingProgress();
628        finish();
629    }
630
631    @Override
632    public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) {
633        // First, let's tell the SharedImageProvider that it will need to wait
634        // for the image
635        Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI,
636                Uri.encode(mSharedOutputFile.getAbsolutePath()));
637        ContentValues values = new ContentValues();
638        values.put(SharedImageProvider.PREPARE, true);
639        getContentResolver().insert(uri, values);
640        mSharingImage = true;
641
642        // Process and save the image in the background.
643        showSavingProgress(null);
644        mImageShow.saveImage(this, mSharedOutputFile);
645        return true;
646    }
647
648    private Intent getDefaultShareIntent() {
649        Intent intent = new Intent(Intent.ACTION_SEND);
650        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
651        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
652        intent.setType(SharedImageProvider.MIME_TYPE);
653        mSharedOutputFile = SaveCopyTask.getNewFile(this, mImageLoader.getUri());
654        Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI,
655                Uri.encode(mSharedOutputFile.getAbsolutePath()));
656        intent.putExtra(Intent.EXTRA_STREAM, uri);
657        return intent;
658    }
659
660    @Override
661    public boolean onCreateOptionsMenu(Menu menu) {
662        getMenuInflater().inflate(R.menu.filtershow_activity_menu, menu);
663        MenuItem showHistory = menu.findItem(R.id.operationsButton);
664        if (mShowingHistoryPanel) {
665            showHistory.setTitle(R.string.hide_history_panel);
666        } else {
667            showHistory.setTitle(R.string.show_history_panel);
668        }
669        MenuItem showState = menu.findItem(R.id.showImageStateButton);
670        if (mShowingImageStatePanel) {
671            showState.setTitle(R.string.hide_imagestate_panel);
672        } else {
673            showState.setTitle(R.string.show_imagestate_panel);
674        }
675        mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share)
676                .getActionProvider();
677        mShareActionProvider.setShareIntent(getDefaultShareIntent());
678        mShareActionProvider.setOnShareTargetSelectedListener(this);
679
680        MenuItem undoItem = menu.findItem(R.id.undoButton);
681        MenuItem redoItem = menu.findItem(R.id.redoButton);
682        MenuItem resetItem = menu.findItem(R.id.resetHistoryButton);
683        mMasterImage.getHistory().setMenuItems(undoItem, redoItem, resetItem);
684        return true;
685    }
686
687    @Override
688    public void onPause() {
689        super.onPause();
690        if (mShareActionProvider != null) {
691            mShareActionProvider.setOnShareTargetSelectedListener(null);
692        }
693    }
694
695    @Override
696    public void onResume() {
697        super.onResume();
698        if (mShareActionProvider != null) {
699            mShareActionProvider.setOnShareTargetSelectedListener(this);
700        }
701    }
702
703    @Override
704    public boolean onOptionsItemSelected(MenuItem item) {
705        switch (item.getItemId()) {
706            case R.id.undoButton: {
707                mPanelController.resetParameters();
708                HistoryAdapter adapter = mMasterImage.getHistory();
709                int position = adapter.undo();
710                mMasterImage.onHistoryItemClick(position);
711                mImageShow.showToast("Undo");
712                invalidateViews();
713                return true;
714            }
715            case R.id.redoButton: {
716                HistoryAdapter adapter = mMasterImage.getHistory();
717                int position = adapter.redo();
718                mMasterImage.onHistoryItemClick(position);
719                mImageShow.showToast("Redo");
720                invalidateViews();
721                return true;
722            }
723            case R.id.resetHistoryButton: {
724                resetHistory();
725                return true;
726            }
727            case R.id.showImageStateButton: {
728                toggleImageStatePanel();
729                return true;
730            }
731            case R.id.operationsButton: {
732                toggleHistoryPanel();
733                return true;
734            }
735            case android.R.id.home: {
736                saveImage();
737                return true;
738            }
739        }
740        return false;
741    }
742
743    public void enableSave(boolean enable) {
744        if (mSaveButton != null)
745            mSaveButton.setEnabled(enable);
746    }
747
748    public FilterIconButton setupFilterRepresentationButton(FilterRepresentation representation, LinearLayout panel, View button) {
749        LayoutInflater inflater =
750                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
751        FilterIconButton icon = (FilterIconButton) inflater.inflate(R.layout.filtericonbutton,
752                panel, false);
753        if (representation.getTextId() != 0) {
754            representation.setName(getString(representation.getTextId()));
755        }
756        String text = representation.getName();
757        icon.setup(text, mPanelController, panel);
758        icon.setFilterRepresentation(representation);
759        icon.setId(representation.getEditorId());
760        mPanelController.addComponent(button, icon);
761        panel.addView(icon);
762        return icon;
763    }
764
765    private void fillFx(LinearLayout listFilters, int buttonId) {
766        // TODO: use listview
767        // TODO: load the filters straight from the filesystem
768
769        FilterFxRepresentation[] fxArray = new FilterFxRepresentation[18];
770        int p = 0;
771
772        int[] drawid = {
773                R.drawable.filtershow_fx_0005_punch,
774//                R.drawable.filtershow_fx_0000_vintage,
775//                R.drawable.filtershow_fx_0004_bw_contrast,
776                R.drawable.filtershow_fx_0002_bleach,
777                R.drawable.filtershow_fx_0001_instant,
778                R.drawable.filtershow_fx_0007_washout,
779                R.drawable.filtershow_fx_0003_blue_crush,
780                R.drawable.filtershow_fx_0008_washout_color,
781                R.drawable.filtershow_fx_0006_x_process
782        };
783
784        int[] fxNameid = {
785                R.string.ffx_punch,
786//                R.string.ffx_vintage,
787//                R.string.ffx_bw_contrast,
788                R.string.ffx_bleach,
789                R.string.ffx_instant,
790                R.string.ffx_washout,
791                R.string.ffx_blue_crush,
792                R.string.ffx_washout_color,
793                R.string.ffx_x_process
794        };
795
796        for (int i = 0; i < drawid.length; i++) {
797            FilterFxRepresentation fx = new FilterFxRepresentation(getString(fxNameid[i]), drawid[i], fxNameid[i]);
798            fxArray[p++] = fx;
799        }
800
801        ImageButton button = (ImageButton) findViewById(buttonId);
802
803        FilterFxRepresentation nullFx = new FilterFxRepresentation(getString(R.string.none), 0, R.string.none);
804        mNullFxFilter = setupFilterRepresentationButton(nullFx, listFilters, button);
805        mNullFxFilter.setSelected(true);
806
807        Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>();
808        FiltersManager.getManager().addLooks(filtersRepresentations);
809        for (FilterRepresentation representation : filtersRepresentations) {
810            setupFilterRepresentationButton(representation, listFilters, button);
811        }
812
813        for (int i = 0; i < p; i++) {
814            setupFilterRepresentationButton(fxArray[i], listFilters, button);
815        }
816    }
817
818    public void setDefaultPreset() {
819        // Default preset (original)
820        ImagePreset preset = new ImagePreset(getString(R.string.history_original)); // empty
821        preset.setImageLoader(mImageLoader);
822
823        mMasterImage.setPreset(preset, true);
824    }
825
826    // //////////////////////////////////////////////////////////////////////////////
827    // Some utility functions
828    // TODO: finish the cleanup.
829
830    public void invalidateViews() {
831        for (ImageShow views : mImageViews) {
832            views.invalidate();
833            views.updateImage();
834        }
835    }
836
837    public void hideImageViews() {
838        for (View view : mImageViews) {
839            view.setVisibility(View.GONE);
840        }
841        mEditorPlaceHolder.hide();
842    }
843
844    // //////////////////////////////////////////////////////////////////////////////
845    // imageState panel...
846
847    public boolean isShowingHistoryPanel() {
848        return mShowingHistoryPanel;
849    }
850
851    private void toggleImageStatePanel() {
852        invalidateOptionsMenu();
853    }
854
855    private void loadImageStatePanel() {
856        StatePanel statePanel = new StatePanel();
857        if (findViewById(R.id.state_panel_container) != null) {
858            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
859            transaction.replace(R.id.state_panel_container, statePanel, mPanelFragmentTag);
860            transaction.commit();
861        }
862    }
863
864    @Override
865    public void onConfigurationChanged(Configuration newConfig)
866    {
867        super.onConfigurationChanged(newConfig);
868        setDefaultValues();
869        loadXML();
870        if (getResources().getConfiguration().orientation
871                == Configuration.ORIENTATION_LANDSCAPE) {
872            mShowingImageStatePanel = true;
873        } else if (mShowingImageStatePanel) {
874            toggleImageStatePanel();
875        }
876        if (mShowingImageStatePanel) {
877            loadImageStatePanel();
878        }
879        if (mShowingHistoryPanel) {
880            toggleHistoryPanel();
881        }
882        if (mShowingTinyPlanet == false) {
883            View tinyPlanetView = findViewById(EditorTinyPlanet.ID);
884            if (tinyPlanetView != null) {
885                tinyPlanetView.setVisibility(View.GONE);
886            }
887        }
888        final View loading = findViewById(R.id.loading);
889        loading.setVisibility(View.GONE);
890    }
891
892    public void setupMasterImage() {
893        mImageLoader = new ImageLoader(this, getApplicationContext());
894
895        HistoryAdapter mHistoryAdapter = new HistoryAdapter(
896                this, R.layout.filtershow_history_operation_row,
897                R.id.rowTextView);
898
899        StateAdapter mImageStateAdapter = new StateAdapter(this, 0);
900        MasterImage.reset();
901        mMasterImage = MasterImage.getImage();
902        mMasterImage.setHistoryAdapter(mHistoryAdapter);
903        mMasterImage.setStateAdapter(mImageStateAdapter);
904        mMasterImage.setActivity(this);
905        mMasterImage.setImageLoader(mImageLoader);
906
907        if (Runtime.getRuntime().maxMemory() > LIMIT_SUPPORTS_HIGHRES) {
908            mMasterImage.setSupportsHighRes(true);
909        } else {
910            mMasterImage.setSupportsHighRes(false);
911        }
912    }
913
914    // //////////////////////////////////////////////////////////////////////////////
915    // history panel...
916
917    public void toggleHistoryPanel() {
918        final View view = findViewById(R.id.mainPanel);
919        final View viewList = findViewById(R.id.historyPanel);
920
921        int translate = translateMainPanel(viewList);
922        if (!mShowingHistoryPanel) {
923            mShowingHistoryPanel = true;
924            if (getResources().getConfiguration().orientation
925                    == Configuration.ORIENTATION_PORTRAIT) {
926                // If portrait, always remove the state panel
927                mShowingImageStatePanel = false;
928                if (PanelController.useAnimationsLayer()) {
929                    view.animate().setDuration(200).x(translate)
930                            .withLayer().withEndAction(new Runnable() {
931                        @Override
932                        public void run() {
933                            viewList.setAlpha(0);
934                            viewList.setVisibility(View.VISIBLE);
935                            viewList.animate().setDuration(100)
936                                    .alpha(1.0f).start();
937                        }
938                    }).start();
939                } else {
940                    view.setX(translate);
941                    viewList.setAlpha(0);
942                    viewList.setVisibility(View.VISIBLE);
943                    viewList.animate().setDuration(100)
944                            .alpha(1.0f).start();
945                }
946            } else {
947                findViewById(R.id.filtersPanel).setVisibility(View.GONE);
948                viewList.setVisibility(View.VISIBLE);
949            }
950        } else {
951            mShowingHistoryPanel = false;
952            if (getResources().getConfiguration().orientation
953                    == Configuration.ORIENTATION_PORTRAIT) {
954                viewList.setVisibility(View.INVISIBLE);
955                if (PanelController.useAnimationsLayer()) {
956                    view.animate().setDuration(200).x(0).withLayer()
957                            .start();
958                } else {
959                    view.setX(0);
960                }
961            } else {
962                viewList.setVisibility(View.GONE);
963                findViewById(R.id.filtersPanel).setVisibility(View.VISIBLE);
964            }
965        }
966        invalidateOptionsMenu();
967    }
968
969    public void dispatchNullFilterClick() {
970        mNullFxFilter.onClick(mNullFxFilter);
971        mNullBorderFilter.onClick(mNullBorderFilter);
972    }
973
974    void resetHistory() {
975        dispatchNullFilterClick();
976        HistoryAdapter adapter = mMasterImage.getHistory();
977        adapter.reset();
978        ImagePreset original = new ImagePreset(adapter.getItem(0));
979        mMasterImage.setPreset(original, true);
980        mPanelController.resetParameters();
981        invalidateViews();
982    }
983
984    // reset button in the history panel.
985    private OnClickListener createOnClickResetOperationsButton() {
986        return new View.OnClickListener() {
987            @Override
988            public void onClick(View v) {
989                resetHistory();
990            }
991        };
992    }
993
994    @Override
995    public void onBackPressed() {
996        if (mPanelController.onBackPressed()) {
997            if (detectSpecialExitCases()) {
998                saveImage();
999            } else if(!mImageShow.hasModifications()) {
1000                done();
1001            } else {
1002                AlertDialog.Builder builder = new AlertDialog.Builder(this);
1003                builder.setMessage(R.string.unsaved).setTitle(R.string.save_before_exit);
1004                builder.setPositiveButton(R.string.save_and_exit, new DialogInterface.OnClickListener() {
1005                    public void onClick(DialogInterface dialog, int id) {
1006                        saveImage();
1007                    }
1008                });
1009                builder.setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
1010                    public void onClick(DialogInterface dialog, int id) {
1011                        done();
1012                    }
1013                });
1014                builder.show();
1015            }
1016        }
1017    }
1018
1019    public PanelController getPanelController() {
1020        return mPanelController;
1021    }
1022
1023    public void cannotLoadImage() {
1024        CharSequence text = getString(R.string.cannot_load_image);
1025        Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
1026        toast.show();
1027        finish();
1028    }
1029
1030    // //////////////////////////////////////////////////////////////////////////////
1031
1032    public float getPixelsFromDip(float value) {
1033        Resources r = getResources();
1034        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
1035                r.getDisplayMetrics());
1036    }
1037
1038    @Override
1039    public void onItemClick(AdapterView<?> parent, View view, int position,
1040            long id) {
1041        mMasterImage.onHistoryItemClick(position);
1042        invalidateViews();
1043    }
1044
1045    public void pickImage() {
1046        Intent intent = new Intent();
1047        intent.setType("image/*");
1048        intent.setAction(Intent.ACTION_GET_CONTENT);
1049        startActivityForResult(Intent.createChooser(intent, getString(R.string.select_image)),
1050                SELECT_PICTURE);
1051    }
1052
1053    @Override
1054    public void onActivityResult(int requestCode, int resultCode, Intent data) {
1055        if (resultCode == RESULT_OK) {
1056            if (requestCode == SELECT_PICTURE) {
1057                Uri selectedImageUri = data.getData();
1058                startLoadBitmap(selectedImageUri);
1059            }
1060        }
1061    }
1062
1063    private boolean mSaveToExtraUri = false;
1064    private boolean mSaveAsWallpaper = false;
1065    private boolean mReturnAsExtra = false;
1066    private boolean mOutputted = false;
1067
1068    public void saveImage() {
1069        handleSpecialExitCases();
1070        if (!mOutputted) {
1071            if (mImageShow.hasModifications()) {
1072                // Get the name of the album, to which the image will be saved
1073                File saveDir = SaveCopyTask.getFinalSaveDirectory(this, mImageLoader.getUri());
1074                int bucketId = GalleryUtils.getBucketId(saveDir.getPath());
1075                String albumName = LocalAlbum.getLocalizedName(getResources(), bucketId, null);
1076                showSavingProgress(albumName);
1077                mImageShow.saveImage(this, null);
1078            } else {
1079                done();
1080            }
1081        }
1082    }
1083
1084    public boolean detectSpecialExitCases() {
1085        return mCropExtras != null && (mCropExtras.getExtraOutput() != null
1086                || mCropExtras.getSetAsWallpaper() || mCropExtras.getReturnData());
1087    }
1088
1089    public void handleSpecialExitCases() {
1090        if (mCropExtras != null) {
1091            if (mCropExtras.getExtraOutput() != null) {
1092                mSaveToExtraUri = true;
1093                mOutputted = true;
1094            }
1095            if (mCropExtras.getSetAsWallpaper()) {
1096                mSaveAsWallpaper = true;
1097                mOutputted = true;
1098            }
1099            if (mCropExtras.getReturnData()) {
1100                mReturnAsExtra = true;
1101                mOutputted = true;
1102            }
1103            if (mOutputted) {
1104                mImageShow.getImagePreset().mGeoData.setUseCropExtrasFlag(true);
1105                showSavingProgress(null);
1106                mImageShow.returnFilteredResult(this);
1107            }
1108        }
1109    }
1110
1111    public void onFilteredResult(Bitmap filtered) {
1112        Intent intent = new Intent();
1113        intent.putExtra(CropExtras.KEY_CROPPED_RECT, mImageShow.getImageCropBounds());
1114        if (mSaveToExtraUri) {
1115            mImageShow.saveToUri(filtered, mCropExtras.getExtraOutput(),
1116                    mCropExtras.getOutputFormat(), this);
1117        }
1118        if (mSaveAsWallpaper) {
1119            setWallpaperInBackground(filtered);
1120        }
1121        if (mReturnAsExtra) {
1122            if (filtered != null) {
1123                int bmapSize = filtered.getRowBytes() * filtered.getHeight();
1124                /*
1125                 * Max size of Binder transaction buffer is 1Mb, so constrain
1126                 * Bitmap to be somewhat less than this, otherwise we get
1127                 * TransactionTooLargeExceptions.
1128                 */
1129                if (bmapSize > MAX_BMAP_IN_INTENT) {
1130                    Log.w(LOGTAG, "Bitmap too large to be returned via intent");
1131                } else {
1132                    intent.putExtra(CropExtras.KEY_DATA, filtered);
1133                }
1134            }
1135        }
1136        setResult(RESULT_OK, intent);
1137        if (!mSaveToExtraUri) {
1138            done();
1139        }
1140    }
1141
1142    void setWallpaperInBackground(final Bitmap bmap) {
1143        Toast.makeText(this, R.string.setting_wallpaper, Toast.LENGTH_LONG).show();
1144        BitmapTask.Callbacks<FilterShowActivity> cb = new BitmapTask.Callbacks<FilterShowActivity>() {
1145            @Override
1146            public void onComplete(Bitmap result) {}
1147
1148            @Override
1149            public void onCancel() {}
1150
1151            @Override
1152            public Bitmap onExecute(FilterShowActivity param) {
1153                try {
1154                    WallpaperManager.getInstance(param).setBitmap(bmap);
1155                } catch (IOException e) {
1156                    Log.w(LOGTAG, "fail to set wall paper", e);
1157                }
1158                return null;
1159            }
1160        };
1161        (new BitmapTask<FilterShowActivity>(cb)).execute(this);
1162    }
1163
1164    public void done() {
1165        if (mOutputted) {
1166            hideSavingProgress();
1167        }
1168        finish();
1169    }
1170
1171    static {
1172        System.loadLibrary("jni_filtershow_filters");
1173    }
1174
1175}
1176