FilterShowActivity.java revision 6fe165b7d28299d5b2f97deb135b233d84eb300f
1a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard/*
2a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Copyright (C) 2012 The Android Open Source Project
3a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
4a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Licensed under the Apache License, Version 2.0 (the "License");
5a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * you may not use this file except in compliance with the License.
6a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * You may obtain a copy of the License at
7a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
8a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *      http://www.apache.org/licenses/LICENSE-2.0
9a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
10a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Unless required by applicable law or agreed to in writing, software
11a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * distributed under the License is distributed on an "AS IS" BASIS,
12a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * See the License for the specific language governing permissions and
14a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * limitations under the License.
15a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard */
160d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
170d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardpackage com.android.gallery3d.filtershow;
180d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
199594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroardimport android.app.ActionBar;
200d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.app.Activity;
21a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunkimport android.app.AlertDialog;
22ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescuimport android.app.ProgressDialog;
236416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunkimport android.app.WallpaperManager;
240ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroardimport android.content.ContentValues;
252e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunkimport android.content.Context;
26a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunkimport android.content.DialogInterface;
270d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.content.Intent;
28a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroardimport android.content.res.Configuration;
290d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.content.res.Resources;
30457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hofordimport android.graphics.Bitmap;
3193dfe7a1df07a64701369b480578410efb740a27nicolasroardimport android.graphics.Color;
32fafb2edbb9928039b887f59685d82593bd79e06aJohn Hofordimport android.graphics.Point;
330d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.graphics.drawable.Drawable;
3492e2341248e99c691f38820503984bc5e2f18811nicolasroardimport android.net.Uri;
353d4f2c7443f6f6015efab769bfaad19263137c67John Hofordimport android.os.AsyncTask;
3692e2341248e99c691f38820503984bc5e2f18811nicolasroardimport android.os.Bundle;
376416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunkimport android.provider.MediaStore;
38fafb2edbb9928039b887f59685d82593bd79e06aJohn Hofordimport android.util.DisplayMetrics;
390d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.util.Log;
400d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.util.TypedValue;
416019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroardimport android.view.*;
420d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.view.View.OnClickListener;
43be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport android.widget.AdapterView;
440d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroardimport android.widget.AdapterView.OnItemClickListener;
45be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport android.widget.FrameLayout;
46be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport android.widget.ImageButton;
47be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport android.widget.LinearLayout;
48be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport android.widget.ListView;
49be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport android.widget.ShareActionProvider;
500ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroardimport android.widget.ShareActionProvider.OnShareTargetSelectedListener;
51be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport android.widget.Toast;
520d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
5392e2341248e99c691f38820503984bc5e2f18811nicolasroardimport com.android.gallery3d.R;
540addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liuimport com.android.gallery3d.data.LocalAlbum;
55560f959b659bb510a6ac9a92831534c5dfcf16benicolasroardimport com.android.gallery3d.filtershow.cache.CachingPipeline;
56cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroardimport com.android.gallery3d.filtershow.cache.FilteringPipeline;
5792e2341248e99c691f38820503984bc5e2f18811nicolasroardimport com.android.gallery3d.filtershow.cache.ImageLoader;
586fe165b7d28299d5b2f97deb135b233d84eb300fRuben Brunkimport com.android.gallery3d.filtershow.crop.CropExtras;
591b1a79f1ccb6f847d017a72fa4c288b2195ffd99John Hofordimport com.android.gallery3d.filtershow.editors.BasicEditor;
60d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.filtershow.editors.EditorCrop;
61915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport com.android.gallery3d.filtershow.editors.EditorDraw;
62d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.filtershow.editors.EditorFlip;
63d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.filtershow.editors.EditorInfo;
645aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport com.android.gallery3d.filtershow.editors.EditorManager;
6512c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hofordimport com.android.gallery3d.filtershow.editors.EditorRedEye;
66d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.filtershow.editors.EditorRotate;
67d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.filtershow.editors.EditorStraighten;
6844a499a64158cd5a12f8e44655f82b68c33bf3b2John Hofordimport com.android.gallery3d.filtershow.editors.EditorTinyPlanet;
69be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hofordimport com.android.gallery3d.filtershow.editors.ImageOnlyEditor;
708d2c7ee02c8fde53effc787c172b10b14ae34d42nicolasroardimport com.android.gallery3d.filtershow.filters.*;
71d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
72de154778c8f94121cb2117fcf6224c40c01b6132nicolasroardimport com.android.gallery3d.filtershow.imageshow.ImageCrop;
7392e2341248e99c691f38820503984bc5e2f18811nicolasroardimport com.android.gallery3d.filtershow.imageshow.ImageShow;
742c6ea941a80ea22317d664e329aed51f5f7417b9nicolasroardimport com.android.gallery3d.filtershow.imageshow.ImageTinyPlanet;
7563a7dac01d394523799939596b960cb03321798dJohn Hofordimport com.android.gallery3d.filtershow.imageshow.MasterImage;
7692e2341248e99c691f38820503984bc5e2f18811nicolasroardimport com.android.gallery3d.filtershow.presets.ImagePreset;
7792e2341248e99c691f38820503984bc5e2f18811nicolasroardimport com.android.gallery3d.filtershow.provider.SharedImageProvider;
78a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunkimport com.android.gallery3d.filtershow.tools.BitmapTask;
7992e2341248e99c691f38820503984bc5e2f18811nicolasroardimport com.android.gallery3d.filtershow.tools.SaveCopyTask;
802e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunkimport com.android.gallery3d.filtershow.ui.FilterIconButton;
817592704e662e49e4bbf3ba86a43d790774829bb0nicolasroardimport com.android.gallery3d.filtershow.ui.FramedTextButton;
823992ae6cf1cbb7b57a22d8287c40451558408335nicolasroardimport com.android.gallery3d.filtershow.ui.Spline;
830addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liuimport com.android.gallery3d.util.GalleryUtils;
84f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunkimport com.android.photos.data.GalleryBitmapPool;
8592e2341248e99c691f38820503984bc5e2f18811nicolasroard
8692e2341248e99c691f38820503984bc5e2f18811nicolasroardimport java.io.File;
876416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunkimport java.io.IOException;
88ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescuimport java.lang.ref.WeakReference;
8992e2341248e99c691f38820503984bc5e2f18811nicolasroardimport java.util.Vector;
9092e2341248e99c691f38820503984bc5e2f18811nicolasroard
910ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroardpublic class FilterShowActivity extends Activity implements OnItemClickListener,
920ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        OnShareTargetSelectedListener {
930d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
946416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    // fields for supporting crop action
95795776f67fe5bca902b09c01edec630879263341Ruben Brunk    public static final String CROP_ACTION = "com.android.camera.action.CROP";
966416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    private CropExtras mCropExtras = null;
97ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk    private String mAction = "";
98588c30c74178bc399b043a5401f0c1f93ae22378nicolasroard    MasterImage mMasterImage = null;
996416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk
1004dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard    private static final long LIMIT_SUPPORTS_HIGHRES = 134217728; // 128Mb
1014dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard
102a680a0c32ae3321c632288b36083f2782af2c55bDoris Liu    public static final String TINY_PLANET_ACTION = "com.android.camera.action.TINY_PLANET";
103a850a543225e9f7a72c5a49bb6099573c7efda13Bobby Georgescu    public static final String LAUNCH_FULLSCREEN = "launch-fullscreen";
1046416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    public static final int MAX_BMAP_IN_INTENT = 990000;
10592e2341248e99c691f38820503984bc5e2f18811nicolasroard    private final PanelController mPanelController = new PanelController();
1060d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    private ImageLoader mImageLoader = null;
1070d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    private ImageShow mImageShow = null;
1082c6ea941a80ea22317d664e329aed51f5f7417b9nicolasroard    private ImageTinyPlanet mImageTinyPlanet = null;
1090d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
110bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu    private View mSaveButton = null;
1110d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
1125aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    private EditorPlaceHolder mEditorPlaceHolder = new EditorPlaceHolder(this);
1135aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1140d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    private static final int SELECT_PICTURE = 1;
1150d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    private static final String LOGTAG = "FilterShowActivity";
11692e2341248e99c691f38820503984bc5e2f18811nicolasroard    protected static final boolean ANIMATE_PANELS = true;
117f129cb70f4d2b43a147b399d91650414defa6dd9nicolasroard    private static int mImageBorderSize = 4; // in percent
1180d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
1195300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford    private boolean mShowingTinyPlanet = false;
1200d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    private boolean mShowingHistoryPanel = false;
121bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard    private boolean mShowingImageStatePanel = false;
122bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard
12392e2341248e99c691f38820503984bc5e2f18811nicolasroard    private final Vector<ImageShow> mImageViews = new Vector<ImageShow>();
1240d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
1250ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    private ShareActionProvider mShareActionProvider;
1260ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    private File mSharedOutputFile = null;
1270ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard
1280ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    private boolean mSharingImage = false;
1290ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard
130ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu    private WeakReference<ProgressDialog> mSavingProgressDialog;
13155550694a3f40fbf5dfc95c35a8e253aedab931enicolasroard
132d49d53206a197b08ec4754d8751b6860e33d9f48John Reck    private LoadBitmapTask mLoadBitmapTask;
1332e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk    private FilterIconButton mNullFxFilter;
1342e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk    private FilterIconButton mNullBorderFilter;
1352e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk    private int mIconSeedSize = 140;
1363d4f2c7443f6f6015efab769bfaad19263137c67John Hoford
1373a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
1380d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    @Override
1390d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public void onCreate(Bundle savedInstanceState) {
1400d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        super.onCreate(savedInstanceState);
1410d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
142f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk        clearGalleryBitmapPool();
143f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk
144560f959b659bb510a6ac9a92831534c5dfcf16benicolasroard        CachingPipeline.createRenderscriptContext(this);
14563a7dac01d394523799939596b960cb03321798dJohn Hoford        setupMasterImage();
1463a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        setDefaultValues();
1473a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        fillEditors();
1485448bf8095483574649afcc2add7f153670c7450nicolasroard
1493a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        loadXML();
150cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard        if (getResources().getConfiguration().orientation
151cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                == Configuration.ORIENTATION_LANDSCAPE) {
152cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard            mShowingImageStatePanel = true;
153cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard        }
154b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard        if (mShowingHistoryPanel) {
155b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard            findViewById(R.id.historyPanel).setVisibility(View.VISIBLE);
156b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard        } else {
157b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard            findViewById(R.id.historyPanel).setVisibility(View.GONE);
158b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard        }
159b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard        if (mShowingImageStatePanel) {
160b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard            findViewById(R.id.imageStatePanel).setVisibility(View.VISIBLE);
161b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard        } else {
162b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard            findViewById(R.id.imageStatePanel).setVisibility(View.GONE);
163b2fcd4252cbcee124e68412bb53a8b875ce9ba36nicolasroard        }
164cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard
1653a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        setDefaultPreset();
1662e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk
1673a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        processIntent();
1683a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
1695ec40a6ba587fa8bd50958d263bb6fc6280de4c1nicolasroard
1703a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void loadXML() {
1710d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        setContentView(R.layout.filtershow_activity);
1723a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
1736f3ff5b486e30221361afd066510a7374962fd09John Hoford        ((ViewStub) findViewById(R.id.stateCategoryStub)).inflate();
1746f3ff5b486e30221361afd066510a7374962fd09John Hoford        ((ViewStub) findViewById(R.id.editorPanelStub)).inflate();
1756f3ff5b486e30221361afd066510a7374962fd09John Hoford        ((ViewStub) findViewById(R.id.historyPanelStub)).inflate();
1766f3ff5b486e30221361afd066510a7374962fd09John Hoford        ((ViewStub) findViewById(R.id.statePanelStub)).inflate();
1776f3ff5b486e30221361afd066510a7374962fd09John Hoford
1789594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        ActionBar actionBar = getActionBar();
179786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
180786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard        actionBar.setCustomView(R.layout.filtershow_actionbar);
181786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard
182bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu        mSaveButton = actionBar.getCustomView();
183bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu        mSaveButton.setOnClickListener(new OnClickListener() {
184786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard            @Override
185786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard            public void onClick(View view) {
186786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard                saveImage();
187786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard            }
188786fe6476e4ccf4b98489ff69d8ef439533cdc2anicolasroard        });
1890d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
1900d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        mImageShow = (ImageShow) findViewById(R.id.imageShow);
1912c6ea941a80ea22317d664e329aed51f5f7417b9nicolasroard        mImageTinyPlanet = (ImageTinyPlanet) findViewById(R.id.imageTinyPlanet);
1920d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        mImageViews.add(mImageShow);
1932c6ea941a80ea22317d664e329aed51f5f7417b9nicolasroard        mImageViews.add(mImageTinyPlanet);
1940d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
1953a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        setupEditors();
1965aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
197733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard        mEditorPlaceHolder.hide();
198733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard
19992e2341248e99c691f38820503984bc5e2f18811nicolasroard        mImageShow.setImageLoader(mImageLoader);
2002c6ea941a80ea22317d664e329aed51f5f7417b9nicolasroard        mImageTinyPlanet.setImageLoader(mImageLoader);
201db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
2023a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mPanelController.clear();
20398ba1a5eb757bb358d75b2f2e5d6fc4c11d805d9nicolasroard        mPanelController.setActivity(this);
2045aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mPanelController.setEditorPlaceHolder(mEditorPlaceHolder);
20598ba1a5eb757bb358d75b2f2e5d6fc4c11d805d9nicolasroard
20692e2341248e99c691f38820503984bc5e2f18811nicolasroard        mPanelController.addImageView(findViewById(R.id.imageShow));
2072c6ea941a80ea22317d664e329aed51f5f7417b9nicolasroard        mPanelController.addImageView(findViewById(R.id.imageTinyPlanet));
20892e2341248e99c691f38820503984bc5e2f18811nicolasroard
2093a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mPanelController.addPanel(R.id.fxButton, R.id.fxList, 0);
2103a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mPanelController.addPanel(R.id.borderButton, R.id.bordersList, 1);
2113a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mPanelController.addPanel(R.id.geometryButton, R.id.geometryList, 2);
2123a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mPanelController.addPanel(R.id.colorsButton, R.id.colorsFxList, 3);
213d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
2143a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        fillFx((LinearLayout) findViewById(R.id.listFilters), R.id.fxButton);
215f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        setupBorders();
21638fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        fillGeometry();
21738fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        fillFilters();
2186900cad45d240c9a54b92991538b6a33652e766cnicolasroard
21992e2341248e99c691f38820503984bc5e2f18811nicolasroard        mPanelController.addView(findViewById(R.id.applyEffect));
2203a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
2216019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroard        setupHistoryPanel();
2226019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroard        setupStatePanel();
2236019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroard    }
2246019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroard
2256019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroard    public void setupHistoryPanel() {
2260d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        findViewById(R.id.resetOperationsButton).setOnClickListener(
2270d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard                createOnClickResetOperationsButton());
2280d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        ListView operationsList = (ListView) findViewById(R.id.operationsList);
22963a7dac01d394523799939596b960cb03321798dJohn Hoford        operationsList.setAdapter(mMasterImage.getHistory());
2300d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        operationsList.setOnItemClickListener(this);
2316019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroard    }
2323a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
2336019ad95f1c85786488d2d4cae1cbb665d33bd30nicolasroard    public void setupStatePanel() {
234bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        ListView imageStateList = (ListView) findViewById(R.id.imageStateList);
23563a7dac01d394523799939596b960cb03321798dJohn Hoford        imageStateList.setAdapter(mMasterImage.getState());
23663a7dac01d394523799939596b960cb03321798dJohn Hoford        mImageLoader.setAdapter(mMasterImage.getHistory());
2376a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        mPanelController.setRowPanel(findViewById(R.id.secondRowPanel));
2386a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        mPanelController.setUtilityPanel(this, findViewById(R.id.filterButtonsList));
2396a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        mPanelController.setCurrentPanel(R.id.fxButton);
2403a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
2413a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
24238fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard    private void fillPanel(Vector<FilterRepresentation> representations, int layoutId, int buttonId) {
24338fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        ImageButton button = (ImageButton) findViewById(buttonId);
24438fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        LinearLayout layout = (LinearLayout) findViewById(layoutId);
24538fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard
24638fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        for (FilterRepresentation representation : representations) {
24738fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard            setupFilterRepresentationButton(representation, layout, button);
24838fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        }
24938fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard    }
25038fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard
2513a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void fillFilters() {
2523a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>();
2533a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        FiltersManager filtersManager = FiltersManager.getManager();
2543a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        filtersManager.addEffects(filtersRepresentations);
25538fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        fillPanel(filtersRepresentations, R.id.listColorsFx, R.id.colorsButton);
2563a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
2573a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
2583a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void fillGeometry() {
25938fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>();
26038fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        FiltersManager filtersManager = FiltersManager.getManager();
26138fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard
2623a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        GeometryMetadata geo = new GeometryMetadata();
2633a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        int[] editorsId = geo.getEditorIds();
2643a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        for (int i = 0; i < editorsId.length; i++) {
2653a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            int editorId = editorsId[i];
2663a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            GeometryMetadata geometry = new GeometryMetadata(geo);
2673a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            geometry.setEditorId(editorId);
2683a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            EditorInfo editorInfo = (EditorInfo) mEditorPlaceHolder.getEditor(editorId);
2693a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            geometry.setTextId(editorInfo.getTextId());
2703a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            geometry.setOverlayId(editorInfo.getOverlayId());
2713a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            geometry.setOverlayOnly(editorInfo.getOverlayOnly());
27238fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard            filtersRepresentations.add(geometry);
2733a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        }
27438fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard
27538fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        filtersManager.addTools(filtersRepresentations);
27638fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        fillPanel(filtersRepresentations, R.id.listGeometry, R.id.geometryButton);
2773a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
2783a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
2793a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void processIntent() {
2800d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        Intent intent = getIntent();
281a850a543225e9f7a72c5a49bb6099573c7efda13Bobby Georgescu        if (intent.getBooleanExtra(LAUNCH_FULLSCREEN, false)) {
282a850a543225e9f7a72c5a49bb6099573c7efda13Bobby Georgescu            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
283a850a543225e9f7a72c5a49bb6099573c7efda13Bobby Georgescu        }
284a850a543225e9f7a72c5a49bb6099573c7efda13Bobby Georgescu
285ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk        mAction = intent.getAction();
286ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk
287d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        if (intent.getData() != null) {
288d49d53206a197b08ec4754d8751b6860e33d9f48John Reck            startLoadBitmap(intent.getData());
2890d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        } else {
2900d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            pickImage();
2910d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        }
29263926d2ed2ca2921de8dca9b3a52c1d64d8afc46Ruben Brunk
2936416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        // Handle behavior for various actions
294ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk        if (mAction.equalsIgnoreCase(CROP_ACTION)) {
2956416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            Bundle extras = intent.getExtras();
2966416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            if (extras != null) {
2976416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mCropExtras = new CropExtras(extras.getInt(CropExtras.KEY_OUTPUT_X, 0),
2986416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getInt(CropExtras.KEY_OUTPUT_Y, 0),
2996416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getBoolean(CropExtras.KEY_SCALE, true) &&
3006416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                                extras.getBoolean(CropExtras.KEY_SCALE_UP_IF_NEEDED, false),
3016416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getInt(CropExtras.KEY_ASPECT_X, 0),
3026416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getInt(CropExtras.KEY_ASPECT_Y, 0),
3036416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getBoolean(CropExtras.KEY_SET_AS_WALLPAPER, false),
3046416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getBoolean(CropExtras.KEY_RETURN_DATA, false),
3056416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        (Uri) extras.getParcelable(MediaStore.EXTRA_OUTPUT),
3066416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getString(CropExtras.KEY_OUTPUT_FORMAT),
3076416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getBoolean(CropExtras.KEY_SHOW_WHEN_LOCKED, false),
3086416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getFloat(CropExtras.KEY_SPOTLIGHT_X),
3096416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        extras.getFloat(CropExtras.KEY_SPOTLIGHT_Y));
3106416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk
3116416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                if (mCropExtras.getShowWhenLocked()) {
3126416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
3136416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                }
3146416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mImageShow.getImagePreset().mGeoData.setCropExtras(mCropExtras);
3156416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk
316d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford                // FIXME: moving to editors breaks the crop action
317be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford                EditorCrop crop = (EditorCrop) mEditorPlaceHolder.getEditor(EditorCrop.ID);
318be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford
319be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford                crop.setExtras(mCropExtras);
3206416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                String s = getString(R.string.Fixed);
321be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford                crop.setAspectString(s);
322be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford                crop.setCropActionFlag(true);
3236416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mPanelController.setFixedAspect(mCropExtras.getAspectX() > 0
3246416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                        && mCropExtras.getAspectY() > 0);
3256416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            }
32663926d2ed2ca2921de8dca9b3a52c1d64d8afc46Ruben Brunk        }
3270d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
3280d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
3293a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void setupEditors() {
3303a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.setContainer((FrameLayout) findViewById(R.id.editorContainer));
3313a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        EditorManager.addEditors(mEditorPlaceHolder);
3323a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.setOldViews(mImageViews);
3333a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.setImageLoader(mImageLoader);
3343a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
3353a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
3363a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void fillEditors() {
3373a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new EditorDraw());
3383a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new BasicEditor());
3393a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new ImageOnlyEditor());
3403a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new EditorTinyPlanet());
3413a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new EditorRedEye());
3423a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new EditorCrop());
3433a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new EditorFlip());
3443a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new EditorRotate());
3453a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mEditorPlaceHolder.addEditor(new EditorStraighten());
3463a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
3473a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
3483a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void setDefaultValues() {
3493a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageFilter.setActivityForMemoryToasts(this);
3503a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
3513a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        Resources res = getResources();
3528d2c7ee02c8fde53effc787c172b10b14ae34d42nicolasroard        // TODO: add a mechanism to set the resources in FiltersManagmer
3533a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageFilterBorder filterBorder = (ImageFilterBorder) FiltersManager.getManager().getFilter(ImageFilterBorder.class);
3543a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        filterBorder.setResources(res);
355402dd963ad8fb1dfed81cca63cbaf82e93533a94nicolasroard        filterBorder = (ImageFilterBorder) FiltersManager.getPreviewManager().getFilter(ImageFilterBorder.class);
356402dd963ad8fb1dfed81cca63cbaf82e93533a94nicolasroard        filterBorder.setResources(res);
3578d2c7ee02c8fde53effc787c172b10b14ae34d42nicolasroard        ImageFilterFx filterFx = (ImageFilterFx) FiltersManager.getManager().getFilter(ImageFilterFx.class);
3588d2c7ee02c8fde53effc787c172b10b14ae34d42nicolasroard        filterFx.setResources(res);
3598d2c7ee02c8fde53effc787c172b10b14ae34d42nicolasroard        filterFx = (ImageFilterFx) FiltersManager.getPreviewManager().getFilter(ImageFilterFx.class);
3608d2c7ee02c8fde53effc787c172b10b14ae34d42nicolasroard        filterFx.setResources(res);
3613a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
3623a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageShow.setDefaultBackgroundColor(res.getColor(R.color.background_screen));
3633a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        // TODO: get those values from XML.
3643a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        FramedTextButton.setTextSize((int) getPixelsFromDip(14));
3653a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        FramedTextButton.setTrianglePadding((int) getPixelsFromDip(4));
3663a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        FramedTextButton.setTriangleSize((int) getPixelsFromDip(10));
3673a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageShow.setTextSize((int) getPixelsFromDip(12));
3683a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageShow.setTextPadding((int) getPixelsFromDip(10));
3693a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageShow.setOriginalTextMargin((int) getPixelsFromDip(4));
3703a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageShow.setOriginalTextSize((int) getPixelsFromDip(18));
3713a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageShow.setOriginalText(res.getString(R.string.original_picture_text));
3723a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mIconSeedSize = res.getDimensionPixelSize(R.dimen.thumbnail_size);
3734e1d35506bb72a2a6c16f08f8bd86bdfe8d1c902nicolasroard        // TODO: pick correct value
3744e1d35506bb72a2a6c16f08f8bd86bdfe8d1c902nicolasroard        // MasterImage.setIconSeedSize(mIconSeedSize);
3753a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
3763a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        Drawable curveHandle = res.getDrawable(R.drawable.camera_crop);
3773a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        int curveHandleSize = (int) res.getDimension(R.dimen.crop_indicator_size);
3783a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        Spline.setCurveHandle(curveHandle, curveHandleSize);
3793a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        Spline.setCurveWidth((int) getPixelsFromDip(3));
3803a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
3813a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageCrop.setAspectTextSize((int) getPixelsFromDip(18));
3823a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageCrop.setTouchTolerance((int) getPixelsFromDip(25));
3833a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageCrop.setMinCropSize((int) getPixelsFromDip(55));
3843a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
3853a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
386d49d53206a197b08ec4754d8751b6860e33d9f48John Reck    private void startLoadBitmap(Uri uri) {
38700914f56c3390caa224b1c9fc07624665de40113John Reck        final View filters = findViewById(R.id.filtersPanel);
38800914f56c3390caa224b1c9fc07624665de40113John Reck        final View loading = findViewById(R.id.loading);
389733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard        final View imageShow = findViewById(R.id.imageShow);
390733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard        imageShow.setVisibility(View.INVISIBLE);
39100914f56c3390caa224b1c9fc07624665de40113John Reck        filters.setVisibility(View.INVISIBLE);
392733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard        loading.setVisibility(View.VISIBLE);
393733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard
394d4653680279a85720f792f21e32578ccf5c6c34fJohn Hoford        View tinyPlanetView = findViewById(EditorTinyPlanet.ID);
395d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        if (tinyPlanetView != null) {
3965300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford            mShowingTinyPlanet = false;
397d49d53206a197b08ec4754d8751b6860e33d9f48John Reck            tinyPlanetView.setVisibility(View.GONE);
398d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        }
399d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        mLoadBitmapTask = new LoadBitmapTask(tinyPlanetView);
400d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        mLoadBitmapTask.execute(uri);
401d49d53206a197b08ec4754d8751b6860e33d9f48John Reck    }
402d49d53206a197b08ec4754d8751b6860e33d9f48John Reck
403f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk    private void setupBorders() {
404f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        LinearLayout list = (LinearLayout) findViewById(R.id.listBorders);
405f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        Vector<FilterRepresentation> borders = new Vector<FilterRepresentation>();
406d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk        ImageButton borderButton = (ImageButton) findViewById(R.id.borderButton);
407d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk
408d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk        // The "no border" implementation
409f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterImageBorderRepresentation(0));
410d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk
411d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk        // Google-build borders
412d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk        FiltersManager.getManager().addBorders(borders);
413d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk
414d88683e307eb058e71c083bb795bd31945ed1292Ruben Brunk        // Regular borders
415f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_4x5));
416f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_brush));
417f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_grunge));
418f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_sumi_e));
419f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_tape));
420f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterColorBorderRepresentation(Color.BLACK, mImageBorderSize, 0));
421f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterColorBorderRepresentation(Color.BLACK, mImageBorderSize,
422f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk                mImageBorderSize));
423f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterColorBorderRepresentation(Color.WHITE, mImageBorderSize, 0));
424f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterColorBorderRepresentation(Color.WHITE, mImageBorderSize,
425f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk                mImageBorderSize));
426f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        int creamColor = Color.argb(255, 237, 237, 227);
427f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterColorBorderRepresentation(creamColor, mImageBorderSize, 0));
428f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        borders.add(new FilterColorBorderRepresentation(creamColor, mImageBorderSize,
429f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk                mImageBorderSize));
430f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        for (int i = 0; i < borders.size(); i++) {
431f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk            FilterRepresentation filter = borders.elementAt(i);
432f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk            filter.setName(getString(R.string.borders));
433f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk            if (i == 0) {
434f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk                filter.setName(getString(R.string.none));
4359090141f56b4e2000704d0c4a778d3a819821726nicolasroard            }
436f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk            FilterIconButton b = setupFilterRepresentationButton(filter, list, borderButton);
437f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk            if (i == 0) {
438f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk                mNullBorderFilter = b;
439f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk                mNullBorderFilter.setSelected(true);
4409090141f56b4e2000704d0c4a778d3a819821726nicolasroard            }
4419090141f56b4e2000704d0c4a778d3a819821726nicolasroard        }
4429090141f56b4e2000704d0c4a778d3a819821726nicolasroard    }
4439090141f56b4e2000704d0c4a778d3a819821726nicolasroard
4448965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk    private class LoadBitmapTask extends AsyncTask<Uri, Boolean, Boolean> {
4453d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        View mTinyPlanetButton;
446d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        int mBitmapSize;
4473d4f2c7443f6f6015efab769bfaad19263137c67John Hoford
448d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        public LoadBitmapTask(View button) {
4493d4f2c7443f6f6015efab769bfaad19263137c67John Hoford            mTinyPlanetButton = button;
450d49d53206a197b08ec4754d8751b6860e33d9f48John Reck            mBitmapSize = getScreenImageSize();
4513d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        }
4523d4f2c7443f6f6015efab769bfaad19263137c67John Hoford
4533d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        @Override
454d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        protected Boolean doInBackground(Uri... params) {
4558965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk            if (!mImageLoader.loadBitmap(params[0], mBitmapSize)) {
4568965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk                return false;
4578965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk            }
4588965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk            publishProgress(mImageLoader.queryLightCycle360());
4598965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk            return true;
4603d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        }
4613d4f2c7443f6f6015efab769bfaad19263137c67John Hoford
4623d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        @Override
4638965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk        protected void onProgressUpdate(Boolean... values) {
46400914f56c3390caa224b1c9fc07624665de40113John Reck            super.onProgressUpdate(values);
4656416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            if (isCancelled()) {
4668965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk                return;
4676416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            }
4688965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk            if (values[0]) {
4695300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford                mShowingTinyPlanet = true;
4708965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk                mTinyPlanetButton.setVisibility(View.VISIBLE);
4718965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk            }
47200914f56c3390caa224b1c9fc07624665de40113John Reck        }
47300914f56c3390caa224b1c9fc07624665de40113John Reck
47400914f56c3390caa224b1c9fc07624665de40113John Reck        @Override
4753d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        protected void onPostExecute(Boolean result) {
476588c30c74178bc399b043a5401f0c1f93ae22378nicolasroard
4773d4f2c7443f6f6015efab769bfaad19263137c67John Hoford            if (isCancelled()) {
4783d4f2c7443f6f6015efab769bfaad19263137c67John Hoford                return;
4793d4f2c7443f6f6015efab769bfaad19263137c67John Hoford            }
480588c30c74178bc399b043a5401f0c1f93ae22378nicolasroard
4818965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk            if (!result) {
4828965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk                cannotLoadImage();
4833d4f2c7443f6f6015efab769bfaad19263137c67John Hoford            }
4848965d1f4c2d437e0a0ad4fd225ea2cad9d2471c4Ruben Brunk
485733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard            final View loading = findViewById(R.id.loading);
486733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard            loading.setVisibility(View.GONE);
487733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard            final View filters = findViewById(R.id.filtersPanel);
488733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard            filters.setVisibility(View.VISIBLE);
489b8270a94e539fc1944ae350c73b00b9df45832d5nicolasroard            if (PanelController.useAnimationsLayer()) {
490d31603218b1f5f58a3617a21425622790ffff472nicolasroard                float y = filters.getY();
491d31603218b1f5f58a3617a21425622790ffff472nicolasroard                filters.setY(y + filters.getHeight());
492d31603218b1f5f58a3617a21425622790ffff472nicolasroard                filters.animate().setDuration(600).y(y).withLayer().start();
493d31603218b1f5f58a3617a21425622790ffff472nicolasroard            }
494733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard            final View imageShow = findViewById(R.id.imageShow);
495733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard            imageShow.setVisibility(View.VISIBLE);
496733a013ae1f2fe9979fb5a277b98af1544b8afb7nicolasroard
497cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            Bitmap largeBitmap = mImageLoader.getOriginalBitmapLarge();
498cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            FilteringPipeline pipeline = FilteringPipeline.getPipeline();
499cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            pipeline.setOriginal(largeBitmap);
500cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            float previewScale = (float) largeBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width();
501cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            pipeline.setPreviewScaleFactor(previewScale);
5024dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard            Bitmap highresBitmap = mImageLoader.getOriginalBitmapHighres();
5034dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard            if (highresBitmap != null) {
5044dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard                float highResPreviewScale = (float) highresBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width();
5054dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard                pipeline.setHighResPreviewScaleFactor(highResPreviewScale);
5064dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard            }
50733fca9ef7f89b60e3b1a361fdf5fc597b3994e74nicolasroard            pipeline.turnOnPipeline(true);
508be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford            MasterImage.getImage().setOriginalGeometry(largeBitmap);
509d49d53206a197b08ec4754d8751b6860e33d9f48John Reck            mLoadBitmapTask = null;
510ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk
511ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk            if (mAction == CROP_ACTION) {
512be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford                mPanelController.showComponent(findViewById(EditorCrop.ID));
513ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk            } else if (mAction == TINY_PLANET_ACTION) {
514be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford                mPanelController.showComponent(findViewById(EditorTinyPlanet.ID));
515ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk            }
5163d4f2c7443f6f6015efab769bfaad19263137c67John Hoford            super.onPostExecute(result);
5173d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        }
5183d4f2c7443f6f6015efab769bfaad19263137c67John Hoford
5193d4f2c7443f6f6015efab769bfaad19263137c67John Hoford    }
5203d4f2c7443f6f6015efab769bfaad19263137c67John Hoford
521f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk    private void clearGalleryBitmapPool() {
522f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk        (new AsyncTask<Void, Void, Void>() {
523f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk            @Override
524f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk            protected Void doInBackground(Void... params) {
525f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk                // Free memory held in Gallery's Bitmap pool.  May be O(n) for n bitmaps.
526f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk                GalleryBitmapPool.getInstance().clear();
527f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk                return null;
528f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk            }
529f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk        }).execute();
530f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk    }
531f07bb27436a65b233c1caf4aa06c6257a9cf4046Ruben Brunk
5323d4f2c7443f6f6015efab769bfaad19263137c67John Hoford    @Override
5333d4f2c7443f6f6015efab769bfaad19263137c67John Hoford    protected void onDestroy() {
534d49d53206a197b08ec4754d8751b6860e33d9f48John Reck        if (mLoadBitmapTask != null) {
535d49d53206a197b08ec4754d8751b6860e33d9f48John Reck            mLoadBitmapTask.cancel(false);
5363d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        }
53721a01faab1125c5c347a3dfbf6a71de6aec233a1Ruben Brunk        // TODO:  Using singletons is a bad design choice for many of these
53821a01faab1125c5c347a3dfbf6a71de6aec233a1Ruben Brunk        // due static reference leaks and in general.  Please refactor.
5398898eaabfa0cdf70e00078d2dea810f9aab4dfd1Ruben Brunk        FilteringPipeline.getPipeline().turnOnPipeline(false);
540b17b1b0758799914fbad5f4a3afae71ce58891fcRuben Brunk        MasterImage.reset();
541b17b1b0758799914fbad5f4a3afae71ce58891fcRuben Brunk        FilteringPipeline.reset();
542b17b1b0758799914fbad5f4a3afae71ce58891fcRuben Brunk        ImageFilter.resetStatics();
543f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        FiltersManager.getPreviewManager().freeRSFilterScripts();
544f79f954f4eb23bce55061c683a07fc7b3a965451Ruben Brunk        FiltersManager.getManager().freeRSFilterScripts();
54521a01faab1125c5c347a3dfbf6a71de6aec233a1Ruben Brunk        FiltersManager.reset();
546560f959b659bb510a6ac9a92831534c5dfcf16benicolasroard        CachingPipeline.destroyRenderScriptContext();
5473d4f2c7443f6f6015efab769bfaad19263137c67John Hoford        super.onDestroy();
5483d4f2c7443f6f6015efab769bfaad19263137c67John Hoford    }
5493d4f2c7443f6f6015efab769bfaad19263137c67John Hoford
5504d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard    private int translateMainPanel(View viewPanel) {
5514d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        int accessoryPanelWidth = viewPanel.getWidth();
5524d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        int mainViewWidth = findViewById(R.id.mainView).getWidth();
5534d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        int mainPanelWidth = mImageShow.getDisplayedImageBounds().width();
554f0eb72eab7be3183417deff6dadb4937b90b9e59nicolasroard        if (mainPanelWidth == 0) {
555f0eb72eab7be3183417deff6dadb4937b90b9e59nicolasroard            mainPanelWidth = mainViewWidth;
556f0eb72eab7be3183417deff6dadb4937b90b9e59nicolasroard        }
557cd84d67629075795d4c5c28d980f77ff8d75c593nicolasroard        int filtersPanelWidth = findViewById(R.id.filtersPanel).getWidth();
558cd84d67629075795d4c5c28d980f77ff8d75c593nicolasroard        if (mainPanelWidth < filtersPanelWidth) {
559cd84d67629075795d4c5c28d980f77ff8d75c593nicolasroard            mainPanelWidth = filtersPanelWidth;
560cd84d67629075795d4c5c28d980f77ff8d75c593nicolasroard        }
5614d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        int leftOver = mainViewWidth - mainPanelWidth - accessoryPanelWidth;
5624d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        if (leftOver < 0) {
5634d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard            return -accessoryPanelWidth;
5644d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        }
5654d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        return 0;
5664d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard    }
5674d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard
5680f7dc6ef6e736c0993240450b50b91721c79c43eRuben Brunk    private int getScreenImageSize() {
5690f7dc6ef6e736c0993240450b50b91721c79c43eRuben Brunk        DisplayMetrics metrics = new DisplayMetrics();
570fafb2edbb9928039b887f59685d82593bd79e06aJohn Hoford        Display display = getWindowManager().getDefaultDisplay();
5710f7dc6ef6e736c0993240450b50b91721c79c43eRuben Brunk        Point size = new Point();
572fafb2edbb9928039b887f59685d82593bd79e06aJohn Hoford        display.getSize(size);
573fafb2edbb9928039b887f59685d82593bd79e06aJohn Hoford        display.getMetrics(metrics);
574fafb2edbb9928039b887f59685d82593bd79e06aJohn Hoford        int msize = Math.min(size.x, size.y);
5750f7dc6ef6e736c0993240450b50b91721c79c43eRuben Brunk        return (133 * msize) / metrics.densityDpi;
576fafb2edbb9928039b887f59685d82593bd79e06aJohn Hoford    }
577fafb2edbb9928039b887f59685d82593bd79e06aJohn Hoford
5780addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu    private void showSavingProgress(String albumName) {
579ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        ProgressDialog progress;
580ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        if (mSavingProgressDialog != null) {
581ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu            progress = mSavingProgressDialog.get();
582ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu            if (progress != null) {
583ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu                progress.show();
584ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu                return;
585ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu            }
586ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        }
587ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        // TODO: Allow cancellation of the saving process
5880addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu        String progressText;
5890addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu        if (albumName == null) {
5900addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu            progressText = getString(R.string.saving_image);
5910addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu        } else {
5920addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu            progressText = getString(R.string.filtershow_saving_image, albumName);
5930addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu        }
5940addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu        progress = ProgressDialog.show(this, "", progressText, true, false);
595ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        mSavingProgressDialog = new WeakReference<ProgressDialog>(progress);
596ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu    }
597ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu
598ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu    private void hideSavingProgress() {
599ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        if (mSavingProgressDialog != null) {
600ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu            ProgressDialog progress = mSavingProgressDialog.get();
60108458dc637d92ee5f12e34d49c34cc10f4e19c7dnicolasroard            if (progress != null)
60208458dc637d92ee5f12e34d49c34cc10f4e19c7dnicolasroard                progress.dismiss();
603ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        }
604ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu    }
605ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu
6060ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    public void completeSaveImage(Uri saveUri) {
6070ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        if (mSharingImage && mSharedOutputFile != null) {
6080ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard            // Image saved, we unblock the content provider
6090ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard            Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI,
6100ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard                    Uri.encode(mSharedOutputFile.getAbsolutePath()));
6110ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard            ContentValues values = new ContentValues();
6120ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard            values.put(SharedImageProvider.PREPARE, false);
6130ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard            getContentResolver().insert(uri, values);
6140ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        }
6150ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        setResult(RESULT_OK, new Intent().setData(saveUri));
616ec29b73d77a8ff2f000eb04d662d85d63d8f8d09Bobby Georgescu        hideSavingProgress();
6170ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        finish();
6180ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    }
6190ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard
6200ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    @Override
6210ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) {
62292e2341248e99c691f38820503984bc5e2f18811nicolasroard        // First, let's tell the SharedImageProvider that it will need to wait
62392e2341248e99c691f38820503984bc5e2f18811nicolasroard        // for the image
6240ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI,
6250ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard                Uri.encode(mSharedOutputFile.getAbsolutePath()));
6260ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        ContentValues values = new ContentValues();
6270ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        values.put(SharedImageProvider.PREPARE, true);
6280ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        getContentResolver().insert(uri, values);
6290ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        mSharingImage = true;
6300ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard
6310ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        // Process and save the image in the background.
6320addfc7f6342184a67cdd8b5cc3872c6a5c87e55Doris Liu        showSavingProgress(null);
6330ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        mImageShow.saveImage(this, mSharedOutputFile);
6340ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        return true;
6350ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    }
6360ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard
6370ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    private Intent getDefaultShareIntent() {
6380ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        Intent intent = new Intent(Intent.ACTION_SEND);
6390ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
6400ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
6410ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        intent.setType(SharedImageProvider.MIME_TYPE);
6420ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        mSharedOutputFile = SaveCopyTask.getNewFile(this, mImageLoader.getUri());
6430ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI,
6440ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard                Uri.encode(mSharedOutputFile.getAbsolutePath()));
6450ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        intent.putExtra(Intent.EXTRA_STREAM, uri);
6460ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        return intent;
6470ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard    }
6480ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard
6499594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard    @Override
6509594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard    public boolean onCreateOptionsMenu(Menu menu) {
6519594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        getMenuInflater().inflate(R.menu.filtershow_activity_menu, menu);
652bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        MenuItem showHistory = menu.findItem(R.id.operationsButton);
6539594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        if (mShowingHistoryPanel) {
654bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            showHistory.setTitle(R.string.hide_history_panel);
655bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        } else {
656bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            showHistory.setTitle(R.string.show_history_panel);
657bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        }
658bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        MenuItem showState = menu.findItem(R.id.showImageStateButton);
659bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        if (mShowingImageStatePanel) {
660bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            showState.setTitle(R.string.hide_imagestate_panel);
6619594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        } else {
662bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            showState.setTitle(R.string.show_imagestate_panel);
6639594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        }
6640ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share)
6650ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard                .getActionProvider();
6660ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        mShareActionProvider.setShareIntent(getDefaultShareIntent());
6670ee91a2b74808fa3f51d370daaddf99f588d72efnicolasroard        mShareActionProvider.setOnShareTargetSelectedListener(this);
66823e58b3595de28bc8f899f3fba1ca9734533213cnicolasroard
66923e58b3595de28bc8f899f3fba1ca9734533213cnicolasroard        MenuItem undoItem = menu.findItem(R.id.undoButton);
67023e58b3595de28bc8f899f3fba1ca9734533213cnicolasroard        MenuItem redoItem = menu.findItem(R.id.redoButton);
67123e58b3595de28bc8f899f3fba1ca9734533213cnicolasroard        MenuItem resetItem = menu.findItem(R.id.resetHistoryButton);
67263a7dac01d394523799939596b960cb03321798dJohn Hoford        mMasterImage.getHistory().setMenuItems(undoItem, redoItem, resetItem);
6739594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        return true;
6749594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard    }
6759594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard
67692e2341248e99c691f38820503984bc5e2f18811nicolasroard    @Override
6776e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu    public void onPause() {
6786e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu        super.onPause();
6796e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu        if (mShareActionProvider != null) {
6806e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu            mShareActionProvider.setOnShareTargetSelectedListener(null);
6816e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu        }
6826e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu    }
6836e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu
6846e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu    @Override
6856e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu    public void onResume() {
6866e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu        super.onResume();
6876e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu        if (mShareActionProvider != null) {
6886e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu            mShareActionProvider.setOnShareTargetSelectedListener(this);
6896e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu        }
6906e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu    }
6916e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu
6926e733b74465ad6994c23d624fd8ee9b7c249e7b3Bobby Georgescu    @Override
6939594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard    public boolean onOptionsItemSelected(MenuItem item) {
6949594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        switch (item.getItemId()) {
6959594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            case R.id.undoButton: {
69619d2d157146841442feaef6e0be002581b69335bJohn Hoford                mPanelController.resetParameters();
69763a7dac01d394523799939596b960cb03321798dJohn Hoford                HistoryAdapter adapter = mMasterImage.getHistory();
6989594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                int position = adapter.undo();
69963a7dac01d394523799939596b960cb03321798dJohn Hoford                mMasterImage.onHistoryItemClick(position);
7009594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                mImageShow.showToast("Undo");
7019594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                invalidateViews();
7029594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                return true;
7039594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            }
7049594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            case R.id.redoButton: {
70563a7dac01d394523799939596b960cb03321798dJohn Hoford                HistoryAdapter adapter = mMasterImage.getHistory();
7069594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                int position = adapter.redo();
70763a7dac01d394523799939596b960cb03321798dJohn Hoford                mMasterImage.onHistoryItemClick(position);
7089594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                mImageShow.showToast("Redo");
7099594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                invalidateViews();
7109594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                return true;
7119594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            }
712f47e790e3f3553f69abe054eec20bd7585118843nicolasroard            case R.id.resetHistoryButton: {
713f47e790e3f3553f69abe054eec20bd7585118843nicolasroard                resetHistory();
714f47e790e3f3553f69abe054eec20bd7585118843nicolasroard                return true;
715f47e790e3f3553f69abe054eec20bd7585118843nicolasroard            }
716bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            case R.id.showImageStateButton: {
717bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard                toggleImageStatePanel();
718bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard                return true;
719bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            }
7209594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            case R.id.operationsButton: {
7219594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                toggleHistoryPanel();
7229594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                return true;
7239594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            }
7249594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            case android.R.id.home: {
7259594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                saveImage();
7269594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                return true;
7279594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            }
7289594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        }
7299594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        return false;
7309594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard    }
7319594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard
732bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu    public void enableSave(boolean enable) {
733bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu        if (mSaveButton != null)
734bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu            mSaveButton.setEnabled(enable);
735bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu    }
736bf1677a434760fcb7389a30c18e05cc3e7926d40Doris Liu
737d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard    public FilterIconButton setupFilterRepresentationButton(FilterRepresentation representation, LinearLayout panel, View button) {
738d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard        LayoutInflater inflater =
739d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
740d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard        FilterIconButton icon = (FilterIconButton) inflater.inflate(R.layout.filtericonbutton,
741d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard                panel, false);
7426900cad45d240c9a54b92991538b6a33652e766cnicolasroard        if (representation.getTextId() != 0) {
7436900cad45d240c9a54b92991538b6a33652e766cnicolasroard            representation.setName(getString(representation.getTextId()));
7446900cad45d240c9a54b92991538b6a33652e766cnicolasroard        }
745d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard        String text = representation.getName();
74631e52aae5eeefd8383cb238e7dd3047e883968efnicolasroard        icon.setup(text, mPanelController, panel);
747d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard        icon.setFilterRepresentation(representation);
748be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford        icon.setId(representation.getEditorId());
7492e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk        mPanelController.addComponent(button, icon);
7502e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk        panel.addView(icon);
7512e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk        return icon;
7522e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk    }
7532e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk
7543a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    private void fillFx(LinearLayout listFilters, int buttonId) {
7550d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        // TODO: use listview
7560d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        // TODO: load the filters straight from the filesystem
757a19830b4fd9050b48386cd904d99cfd2566cc515John Hoford
758d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard        FilterFxRepresentation[] fxArray = new FilterFxRepresentation[18];
7590d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        int p = 0;
760457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford
76108458dc637d92ee5f12e34d49c34cc10f4e19c7dnicolasroard        int[] drawid = {
76243b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.drawable.filtershow_fx_0005_punch,
763457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford                R.drawable.filtershow_fx_0000_vintage,
764457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford                R.drawable.filtershow_fx_0004_bw_contrast,
76543b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.drawable.filtershow_fx_0002_bleach,
76643b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.drawable.filtershow_fx_0001_instant,
767457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford                R.drawable.filtershow_fx_0007_washout,
76843b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.drawable.filtershow_fx_0003_blue_crush,
76943b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.drawable.filtershow_fx_0008_washout_color,
77043b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.drawable.filtershow_fx_0006_x_process
771457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford        };
772457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford
77308458dc637d92ee5f12e34d49c34cc10f4e19c7dnicolasroard        int[] fxNameid = {
77443b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.string.ffx_punch,
775457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford                R.string.ffx_vintage,
776457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford                R.string.ffx_bw_contrast,
77743b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.string.ffx_bleach,
77843b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.string.ffx_instant,
779457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford                R.string.ffx_washout,
78043b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.string.ffx_blue_crush,
781457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford                R.string.ffx_washout_color,
78243b906d81caa4379eae1a1643194c8b3d34b1ad5Ruben Brunk                R.string.ffx_x_process
783457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford        };
784457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford
785457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford        for (int i = 0; i < drawid.length; i++) {
786d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard            FilterFxRepresentation fx = new FilterFxRepresentation(getString(fxNameid[i]), drawid[i], fxNameid[i]);
787d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard            fxArray[p++] = fx;
788457e88cd2ba6a8a93b1643a3fb3d50ee758671adJohn Hoford        }
7892e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk
7903a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImageButton button = (ImageButton) findViewById(buttonId);
7913a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
792d0bab8c396b0b10fdfd07a9d4fccf969d8d46747nicolasroard        FilterFxRepresentation nullFx = new FilterFxRepresentation(getString(R.string.none), 0, R.string.none);
7933a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mNullFxFilter = setupFilterRepresentationButton(nullFx, listFilters, button);
7942e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk        mNullFxFilter.setSelected(true);
7952e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk
796f60760478ec7c78446be0e0c6bc5502d1557974cnicolasroard        Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>();
797f60760478ec7c78446be0e0c6bc5502d1557974cnicolasroard        FiltersManager.getManager().addLooks(filtersRepresentations);
798f60760478ec7c78446be0e0c6bc5502d1557974cnicolasroard        for (FilterRepresentation representation : filtersRepresentations) {
7993a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            setupFilterRepresentationButton(representation, listFilters, button);
800f60760478ec7c78446be0e0c6bc5502d1557974cnicolasroard        }
801f60760478ec7c78446be0e0c6bc5502d1557974cnicolasroard
8020d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        for (int i = 0; i < p; i++) {
8033a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            setupFilterRepresentationButton(fxArray[i], listFilters, button);
8040d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        }
8053a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
8060d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
8073a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    public void setDefaultPreset() {
8080d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        // Default preset (original)
8093a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        ImagePreset preset = new ImagePreset(getString(R.string.history_original)); // empty
8103a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        preset.setImageLoader(mImageLoader);
8113a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
81263a7dac01d394523799939596b960cb03321798dJohn Hoford        mMasterImage.setPreset(preset, true);
8130d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
8140d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
8150d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    // //////////////////////////////////////////////////////////////////////////////
8160d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    // Some utility functions
81792e2341248e99c691f38820503984bc5e2f18811nicolasroard    // TODO: finish the cleanup.
8180d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
8190d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public void invalidateViews() {
8200d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        for (ImageShow views : mImageViews) {
8210d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            views.invalidate();
8228537d097f8827caedc8c39564de54d36eae8b16fRuben Brunk            views.updateImage();
8230d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        }
8240d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
8250d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
8260d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public void hideImageViews() {
8270d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        for (View view : mImageViews) {
8280d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            view.setVisibility(View.GONE);
8290d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        }
8305b85b092af404effd32aa513f9c45a0232d7ae81nicolasroard        mEditorPlaceHolder.hide();
8310d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
8320d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
8330d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    // //////////////////////////////////////////////////////////////////////////////
834bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard    // imageState panel...
835bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard
836d7899c56b8df278dfd6720ae11eadc2f89fe8094nicolasroard    public boolean isShowingHistoryPanel() {
837d7899c56b8df278dfd6720ae11eadc2f89fe8094nicolasroard        return mShowingHistoryPanel;
838d7899c56b8df278dfd6720ae11eadc2f89fe8094nicolasroard    }
839d7899c56b8df278dfd6720ae11eadc2f89fe8094nicolasroard
840bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard    private void toggleImageStatePanel() {
841bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        final View viewList = findViewById(R.id.imageStatePanel);
842bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard
843bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        if (mShowingHistoryPanel) {
844cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard            findViewById(R.id.historyPanel).setVisibility(View.GONE);
845bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            mShowingHistoryPanel = false;
846bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        }
847bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard
848bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        if (!mShowingImageStatePanel) {
849bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            mShowingImageStatePanel = true;
8503a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            viewList.setVisibility(View.VISIBLE);
851bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        } else {
852bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard            mShowingImageStatePanel = false;
8533a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard            viewList.setVisibility(View.GONE);
854bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        }
855bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        invalidateOptionsMenu();
856bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard    }
857bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard
858a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard    @Override
859a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard    public void onConfigurationChanged(Configuration newConfig)
860a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard    {
861a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard        super.onConfigurationChanged(newConfig);
8623a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        setDefaultValues();
8633a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        loadXML();
86463b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard        if (getResources().getConfiguration().orientation
86563b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                == Configuration.ORIENTATION_LANDSCAPE) {
86663b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard            mShowingImageStatePanel = true;
867a2729a16717ec0e7bdaf8fa112d7a1d1760ba2e3nicolasroard        } else if (mShowingImageStatePanel) {
868a2729a16717ec0e7bdaf8fa112d7a1d1760ba2e3nicolasroard            toggleImageStatePanel();
86963b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard        }
870a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard        if (mShowingHistoryPanel) {
871a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard            toggleHistoryPanel();
872a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard        }
8735300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford        if (mShowingTinyPlanet == false) {
8745300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford            View tinyPlanetView = findViewById(EditorTinyPlanet.ID);
8755300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford            if (tinyPlanetView != null) {
8765300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford                tinyPlanetView.setVisibility(View.GONE);
8775300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford            }
8785300e0c54110d67f85fdb2ee9c90be58f50ca4acJohn Hoford        }
8793a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        final View loading = findViewById(R.id.loading);
8803a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        loading.setVisibility(View.GONE);
881a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard    }
882a9bc8ef5774557a66bcb9bb6d38cb16f7ef56e2enicolasroard
88363a7dac01d394523799939596b960cb03321798dJohn Hoford    public void setupMasterImage() {
8843a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        mImageLoader = new ImageLoader(this, getApplicationContext());
8853a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
88663a7dac01d394523799939596b960cb03321798dJohn Hoford        HistoryAdapter mHistoryAdapter = new HistoryAdapter(
88763a7dac01d394523799939596b960cb03321798dJohn Hoford                this, R.layout.filtershow_history_operation_row,
88863a7dac01d394523799939596b960cb03321798dJohn Hoford                R.id.rowTextView);
88963a7dac01d394523799939596b960cb03321798dJohn Hoford        ImageStateAdapter mImageStateAdapter = new ImageStateAdapter(this,
89063a7dac01d394523799939596b960cb03321798dJohn Hoford                R.layout.filtershow_imagestate_row);
89163a7dac01d394523799939596b960cb03321798dJohn Hoford
892588c30c74178bc399b043a5401f0c1f93ae22378nicolasroard        MasterImage.reset();
893588c30c74178bc399b043a5401f0c1f93ae22378nicolasroard        mMasterImage = MasterImage.getImage();
89463a7dac01d394523799939596b960cb03321798dJohn Hoford        mMasterImage.setHistoryAdapter(mHistoryAdapter);
89563a7dac01d394523799939596b960cb03321798dJohn Hoford        mMasterImage.setStateAdapter(mImageStateAdapter);
89663a7dac01d394523799939596b960cb03321798dJohn Hoford        mMasterImage.setActivity(this);
8978d2c7ee02c8fde53effc787c172b10b14ae34d42nicolasroard        mMasterImage.setImageLoader(mImageLoader);
8984dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard
8994dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard        if (Runtime.getRuntime().maxMemory() > LIMIT_SUPPORTS_HIGHRES) {
9004dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard            mMasterImage.setSupportsHighRes(true);
9014dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard        } else {
9024dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard            mMasterImage.setSupportsHighRes(false);
9034dad1a9e3f213e95a4f419c06ea885fee75d3297nicolasroard        }
90463a7dac01d394523799939596b960cb03321798dJohn Hoford    }
90563a7dac01d394523799939596b960cb03321798dJohn Hoford
906bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard    // //////////////////////////////////////////////////////////////////////////////
9070d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    // history panel...
9080d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
909d7899c56b8df278dfd6720ae11eadc2f89fe8094nicolasroard    public void toggleHistoryPanel() {
9109594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        final View view = findViewById(R.id.mainPanel);
9119594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        final View viewList = findViewById(R.id.historyPanel);
912bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard
913bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        if (mShowingImageStatePanel) {
914cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard            findViewById(R.id.imageStatePanel).setVisibility(View.GONE);
915bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard        }
916bf93da72576b28f4e9dfb27f8f3fef702c8ae82dnicolasroard
9174d276f338bbdc53f8a3b4806265bc26c7fe0ea7cnicolasroard        int translate = translateMainPanel(viewList);
9189594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        if (!mShowingHistoryPanel) {
9199594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            mShowingHistoryPanel = true;
920cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard            if (getResources().getConfiguration().orientation
921cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    == Configuration.ORIENTATION_PORTRAIT) {
92263b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                // If portrait, always remove the state panel
92363b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                mShowingImageStatePanel = false;
924b8270a94e539fc1944ae350c73b00b9df45832d5nicolasroard                if (PanelController.useAnimationsLayer()) {
925cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    view.animate().setDuration(200).x(translate)
926cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                            .withLayer().withEndAction(new Runnable() {
92792e2341248e99c691f38820503984bc5e2f18811nicolasroard                        @Override
9289594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                        public void run() {
9299594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                            viewList.setAlpha(0);
9309594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                            viewList.setVisibility(View.VISIBLE);
9319594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                            viewList.animate().setDuration(100)
9329594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                                    .alpha(1.0f).start();
9339594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                        }
9349594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                    }).start();
935cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                } else {
936cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    view.setX(translate);
937cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    viewList.setAlpha(0);
938cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    viewList.setVisibility(View.VISIBLE);
939cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    viewList.animate().setDuration(100)
940cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                            .alpha(1.0f).start();
941cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                }
942d31603218b1f5f58a3617a21425622790ffff472nicolasroard            } else {
94363b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                findViewById(R.id.filtersPanel).setVisibility(View.GONE);
944c364d9f2de28f79c636fcb145c355da52f52b7adJohn Hoford                viewList.setVisibility(View.VISIBLE);
945d31603218b1f5f58a3617a21425622790ffff472nicolasroard            }
9469594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        } else {
9479594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard            mShowingHistoryPanel = false;
948cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard            if (getResources().getConfiguration().orientation
949cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    == Configuration.ORIENTATION_PORTRAIT) {
950cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                viewList.setVisibility(View.INVISIBLE);
951b8270a94e539fc1944ae350c73b00b9df45832d5nicolasroard                if (PanelController.useAnimationsLayer()) {
952cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    view.animate().setDuration(200).x(0).withLayer()
953cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                            .start();
954cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                } else {
955cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                    view.setX(0);
956cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                }
957d31603218b1f5f58a3617a21425622790ffff472nicolasroard            } else {
958cdea10749cbdb6bab8f49f1b5b562b6e89781981nicolasroard                viewList.setVisibility(View.GONE);
95963b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                findViewById(R.id.filtersPanel).setVisibility(View.VISIBLE);
96063b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                // In landscape, bring back the state panel if it was there
96163b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                if (mShowingImageStatePanel) {
96263b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                    findViewById(R.id.imageStatePanel).setVisibility(View.VISIBLE);
96363b33e8c7ebeeec2b520fb419d89ada712ad6604nicolasroard                }
964d31603218b1f5f58a3617a21425622790ffff472nicolasroard            }
9659594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        }
9669594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        invalidateOptionsMenu();
9679594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard    }
9680d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
9692e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk    public void dispatchNullFilterClick() {
970af0dc7f486553fd96b33b15e5380cb1928462d69John Hoford        mNullFxFilter.onClick(mNullFxFilter);
971af0dc7f486553fd96b33b15e5380cb1928462d69John Hoford        mNullBorderFilter.onClick(mNullBorderFilter);
9722e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk    }
973af0dc7f486553fd96b33b15e5380cb1928462d69John Hoford
9742e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk    void resetHistory() {
9752e1a644f97a81931bf9b53d8bdf089bb7d4b1048Ruben Brunk        dispatchNullFilterClick();
97663a7dac01d394523799939596b960cb03321798dJohn Hoford        HistoryAdapter adapter = mMasterImage.getHistory();
9779594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        adapter.reset();
9789594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        ImagePreset original = new ImagePreset(adapter.getItem(0));
97963a7dac01d394523799939596b960cb03321798dJohn Hoford        mMasterImage.setPreset(original, true);
9803992ae6cf1cbb7b57a22d8287c40451558408335nicolasroard        mPanelController.resetParameters();
9819594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard        invalidateViews();
9820d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
9830d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
9840d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    // reset button in the history panel.
9850d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    private OnClickListener createOnClickResetOperationsButton() {
9860d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        return new View.OnClickListener() {
9870d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            @Override
9880d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            public void onClick(View v) {
9899594c8d3e0dd0d6157710ceae1d1deea8e566db4nicolasroard                resetHistory();
9900d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            }
9910d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        };
9920d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
9930d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
99475608166f54891972ebad3d8fd958fed9a5300e6nicolasroard    @Override
99575608166f54891972ebad3d8fd958fed9a5300e6nicolasroard    public void onBackPressed() {
99675608166f54891972ebad3d8fd958fed9a5300e6nicolasroard        if (mPanelController.onBackPressed()) {
997a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk            if (detectSpecialExitCases()) {
998a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                saveImage();
999a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk            } else if(!mImageShow.hasModifications()) {
1000a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                done();
1001a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk            } else {
1002a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                AlertDialog.Builder builder = new AlertDialog.Builder(this);
1003a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                builder.setMessage(R.string.unsaved).setTitle(R.string.save_before_exit);
1004a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                builder.setPositiveButton(R.string.save_and_exit, new DialogInterface.OnClickListener() {
1005a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                    public void onClick(DialogInterface dialog, int id) {
1006a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                        saveImage();
1007a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                    }
1008a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                });
1009e7aa98e533531f5e82f5de37c24701bc44bc6cd1Ruben Brunk                builder.setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
1010a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                    public void onClick(DialogInterface dialog, int id) {
1011a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                        done();
1012a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                    }
1013a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                });
1014e7aa98e533531f5e82f5de37c24701bc44bc6cd1Ruben Brunk                builder.show();
1015a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk            }
101675608166f54891972ebad3d8fd958fed9a5300e6nicolasroard        }
101775608166f54891972ebad3d8fd958fed9a5300e6nicolasroard    }
1018e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard
10193a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    public PanelController getPanelController() {
10203a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard        return mPanelController;
10213a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard    }
10223a5556d3ce5dba33021867468ae0d5d222f256dbnicolasroard
1023e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard    public void cannotLoadImage() {
1024e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard        CharSequence text = getString(R.string.cannot_load_image);
1025e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard        Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
1026e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard        toast.show();
1027e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard        finish();
1028e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard    }
1029e34a5a5eaedb870798a5cc7415079d04a6ca42e0nicolasroard
10300d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    // //////////////////////////////////////////////////////////////////////////////
10310d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
10320d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public float getPixelsFromDip(float value) {
10330d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        Resources r = getResources();
10340d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
10350d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard                r.getDisplayMetrics());
10360d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
10370d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
10380d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    @Override
10390d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public void onItemClick(AdapterView<?> parent, View view, int position,
10400d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            long id) {
104163a7dac01d394523799939596b960cb03321798dJohn Hoford        mMasterImage.onHistoryItemClick(position);
10420d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        invalidateViews();
10430d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
10440d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
10450d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public void pickImage() {
10460d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        Intent intent = new Intent();
10470d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        intent.setType("image/*");
10480d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        intent.setAction(Intent.ACTION_GET_CONTENT);
10490d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        startActivityForResult(Intent.createChooser(intent, getString(R.string.select_image)),
10500d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard                SELECT_PICTURE);
10510d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
10520d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
105392e2341248e99c691f38820503984bc5e2f18811nicolasroard    @Override
10540d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public void onActivityResult(int requestCode, int resultCode, Intent data) {
10550d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        if (resultCode == RESULT_OK) {
10560d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            if (requestCode == SELECT_PICTURE) {
10570d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard                Uri selectedImageUri = data.getData();
1058d49d53206a197b08ec4754d8751b6860e33d9f48John Reck                startLoadBitmap(selectedImageUri);
10590d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard            }
10600d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard        }
10610d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
10620d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
10636416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    private boolean mSaveToExtraUri = false;
10646416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    private boolean mSaveAsWallpaper = false;
10656416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    private boolean mReturnAsExtra = false;
1066ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk    private boolean mOutputted = false;
10676416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk
10680d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    public void saveImage() {
1069a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk        handleSpecialExitCases();
1070a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk        if (!mOutputted) {
1071a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk            if (mImageShow.hasModifications()) {
1072a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                // Get the name of the album, to which the image will be saved
1073a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                File saveDir = SaveCopyTask.getFinalSaveDirectory(this, mImageLoader.getUri());
1074a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                int bucketId = GalleryUtils.getBucketId(saveDir.getPath());
1075a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                String albumName = LocalAlbum.getLocalizedName(getResources(), bucketId, null);
1076a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                showSavingProgress(albumName);
1077a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                mImageShow.saveImage(this, null);
1078a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk            } else {
1079a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                done();
1080a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk            }
1081a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk        }
1082a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk    }
1083a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk
1084a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk    public boolean detectSpecialExitCases() {
1085a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk        return mCropExtras != null && (mCropExtras.getExtraOutput() != null
1086a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk                || mCropExtras.getSetAsWallpaper() || mCropExtras.getReturnData());
1087a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk    }
1088a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk
1089a3dbbd7dcbd9aead7b10653b95685f5d2a71947aRuben Brunk    public void handleSpecialExitCases() {
10906416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        if (mCropExtras != null) {
10916416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            if (mCropExtras.getExtraOutput() != null) {
10926416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mSaveToExtraUri = true;
1093ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk                mOutputted = true;
10946416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            }
10956416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            if (mCropExtras.getSetAsWallpaper()) {
10966416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mSaveAsWallpaper = true;
1097ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk                mOutputted = true;
10986416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            }
10996416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            if (mCropExtras.getReturnData()) {
11006416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mReturnAsExtra = true;
1101ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk                mOutputted = true;
11026416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            }
1103ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk            if (mOutputted) {
11046416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mImageShow.getImagePreset().mGeoData.setUseCropExtrasFlag(true);
11056416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                showSavingProgress(null);
11066416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                mImageShow.returnFilteredResult(this);
11076416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            }
11086416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        }
11096416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    }
11106416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk
11116416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    public void onFilteredResult(Bitmap filtered) {
11126416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        Intent intent = new Intent();
11136416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        intent.putExtra(CropExtras.KEY_CROPPED_RECT, mImageShow.getImageCropBounds());
11146416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        if (mSaveToExtraUri) {
11156416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            mImageShow.saveToUri(filtered, mCropExtras.getExtraOutput(),
11166416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                    mCropExtras.getOutputFormat(), this);
11176416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        }
11186416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        if (mSaveAsWallpaper) {
1119a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            setWallpaperInBackground(filtered);
112055550694a3f40fbf5dfc95c35a8e253aedab931enicolasroard        }
11216416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        if (mReturnAsExtra) {
11226416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            if (filtered != null) {
11236416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                int bmapSize = filtered.getRowBytes() * filtered.getHeight();
11246416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                /*
11256416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                 * Max size of Binder transaction buffer is 1Mb, so constrain
11266416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                 * Bitmap to be somewhat less than this, otherwise we get
11276416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                 * TransactionTooLargeExceptions.
11286416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                 */
11296416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                if (bmapSize > MAX_BMAP_IN_INTENT) {
11306416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                    Log.w(LOGTAG, "Bitmap too large to be returned via intent");
11316416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                } else {
11326416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                    intent.putExtra(CropExtras.KEY_DATA, filtered);
11336416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk                }
11346416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            }
11356416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        }
11366416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        setResult(RESULT_OK, intent);
11376416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        if (!mSaveToExtraUri) {
11386416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            done();
11396416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        }
11406416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    }
11416416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk
1142a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk    void setWallpaperInBackground(final Bitmap bmap) {
1143a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk        Toast.makeText(this, R.string.setting_wallpaper, Toast.LENGTH_LONG).show();
1144c01187e2ef31115518cd1eb900ebf4cfa14e1976Ruben Brunk        BitmapTask.Callbacks<FilterShowActivity> cb = new BitmapTask.Callbacks<FilterShowActivity>() {
1145a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            @Override
1146a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            public void onComplete(Bitmap result) {}
1147a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk
1148a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            @Override
1149a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            public void onCancel() {}
1150a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk
1151a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            @Override
1152a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            public Bitmap onExecute(FilterShowActivity param) {
1153a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk                try {
1154a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk                    WallpaperManager.getInstance(param).setBitmap(bmap);
1155a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk                } catch (IOException e) {
1156a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk                    Log.w(LOGTAG, "fail to set wall paper", e);
1157a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk                }
1158a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk                return null;
1159a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk            }
1160a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk        };
1161c01187e2ef31115518cd1eb900ebf4cfa14e1976Ruben Brunk        (new BitmapTask<FilterShowActivity>(cb)).execute(this);
1162a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk    }
1163a24145829118672e8b9b98e18de78eae71daaa08Ruben Brunk
11646416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk    public void done() {
1165ab779def9c3aa45b100872f2369ec2f4a1d9a709Ruben Brunk        if (mOutputted) {
11666416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk            hideSavingProgress();
11676416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        }
11686416dd59687768d4152d5d954dd0e8c3617b9d97Ruben Brunk        finish();
11690d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
11700d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
11710d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    static {
1172e3f9f579d73af613a33ba1d0a93dc101ea671a69nicolasroard        System.loadLibrary("jni_filtershow_filters");
11730d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard    }
11740d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard
11750d7cdf8e763fb65c32bfad65245b3753deb75737nicolasroard}
1176