1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera.captureintent.state;
18
19import com.google.common.base.Optional;
20
21import com.android.camera.app.AppController;
22import com.android.camera.async.RefCountBase;
23import com.android.camera.captureintent.resource.ResourceConstructed;
24import com.android.camera.captureintent.stateful.State;
25import com.android.camera.captureintent.stateful.StateImpl;
26
27import android.content.Intent;
28
29/**
30 *
31 */
32public final class StateIntentCompleted extends StateImpl {
33    private final RefCountBase<ResourceConstructed> mResourceConstructed;
34    private final Optional<Intent> mResultIntent;
35
36    public static StateIntentCompleted from(
37            StateSavingPicture savingPicture,
38            RefCountBase<ResourceConstructed> resourceConstructed,
39            Intent resultIntent) {
40        return new StateIntentCompleted(
41                savingPicture, resourceConstructed, Optional.of(resultIntent));
42    }
43
44    public static StateIntentCompleted from(
45            State previousState,
46            RefCountBase<ResourceConstructed> resourceConstructed) {
47        return new StateIntentCompleted(
48                previousState, resourceConstructed, Optional.<Intent>absent());
49    }
50
51    private StateIntentCompleted(
52            State previousState,
53            RefCountBase<ResourceConstructed> resourceConstructed,
54            Optional<Intent> resultIntent) {
55        super(previousState);
56        mResourceConstructed = resourceConstructed;
57        mResourceConstructed.addRef();
58        mResultIntent = resultIntent;
59    }
60
61    @Override
62    public Optional<State> onEnter() {
63        final AppController appController = mResourceConstructed.get().getAppController();
64        mResourceConstructed.get().getMainThread().execute(new Runnable() {
65            @Override
66            public void run() {
67                if (mResultIntent.isPresent()) {
68                    appController.finishActivityWithIntentCompleted(mResultIntent.get());
69                } else {
70                    appController.finishActivityWithIntentCanceled();
71                }
72            }
73        });
74        return NO_CHANGE;
75    }
76
77    @Override
78    public void onLeave() {
79    }
80}