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 
17 package com.android.camera.captureintent.state;
18 
19 import com.google.common.base.Optional;
20 
21 import com.android.camera.app.AppController;
22 import com.android.camera.async.RefCountBase;
23 import com.android.camera.captureintent.resource.ResourceConstructed;
24 import com.android.camera.captureintent.stateful.State;
25 import com.android.camera.captureintent.stateful.StateImpl;
26 
27 import android.content.Intent;
28 
29 /**
30  *
31  */
32 public final class StateIntentCompleted extends StateImpl {
33     private final RefCountBase<ResourceConstructed> mResourceConstructed;
34     private final Optional<Intent> mResultIntent;
35 
from( StateSavingPicture savingPicture, RefCountBase<ResourceConstructed> resourceConstructed, Intent resultIntent)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 
from( State previousState, RefCountBase<ResourceConstructed> resourceConstructed)44     public static StateIntentCompleted from(
45             State previousState,
46             RefCountBase<ResourceConstructed> resourceConstructed) {
47         return new StateIntentCompleted(
48                 previousState, resourceConstructed, Optional.<Intent>absent());
49     }
50 
StateIntentCompleted( State previousState, RefCountBase<ResourceConstructed> resourceConstructed, Optional<Intent> resultIntent)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
onEnter()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
onLeave()78     public void onLeave() {
79     }
80 }