1 /*
2  * Copyright (C) 2014 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.ui;
18 
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22 
23 import com.android.camera2.R;
24 
25 /**
26  * Abstract class that is the foundation for a tutorial overlay modules can show
27  * to explain their functionality.
28  */
29 public abstract class AbstractTutorialOverlay {
30     /**
31      * Use this interface to get informed when the tutorial was closed.
32      */
33     public interface CloseListener {
34         /**
35          * Called when the tutorial is being closed.
36          */
onTutorialClosed()37         public void onTutorialClosed();
38     }
39 
40     private final int mLayoutResId;
41     protected final CloseListener mCloseListener;
42     private ViewGroup mPlaceholderWrapper;
43 
44     /**
45      * Create a new overlay.
46      *
47      * @param layoutResId the resource ID of the tutorial layout.
48      * @param inflater The inflater used to inflate the tutorial view.
49      * @param closeListener Called when the user has seen the whole tutorial and
50      *            closed it.
51      */
AbstractTutorialOverlay(int layoutResId, CloseListener closeListener)52     public AbstractTutorialOverlay(int layoutResId, CloseListener closeListener) {
53         mLayoutResId = layoutResId;
54         mCloseListener = closeListener;
55     }
56 
57     /**
58      * Shows the tutorial on the screen.
59      *
60      * @param placeHolderWrapper the view group in which the tutorial will be
61      *            embedded.
62      */
show(ViewGroup placeHolderWrapper, LayoutInflater inflater)63     public final void show(ViewGroup placeHolderWrapper, LayoutInflater inflater) {
64         mPlaceholderWrapper = placeHolderWrapper;
65         if (mPlaceholderWrapper != null) {
66             mPlaceholderWrapper.removeAllViews();
67         }
68 
69         mPlaceholderWrapper.setVisibility(View.VISIBLE);
70         ViewGroup placeHolder = (ViewGroup) inflater.inflate(R.layout.tutorials_placeholder,
71                 mPlaceholderWrapper).findViewById(R.id.tutorials_placeholder);
72         onInflated(inflater.inflate(mLayoutResId, placeHolder));
73     }
74 
75     /**
76      * Called when the view was inflated.
77      *
78      * @param view the inflated tutorial view.
79      */
onInflated(View view)80     protected abstract void onInflated(View view);
81 
82     /**
83      * Removes all views from the place holder wrapper (including the place
84      * holder itself) and sets the visibility of the wrapper to GONE, so that it
85      * doesn't catch any touch events.
86      */
removeOverlayAndHideWrapper()87     public void removeOverlayAndHideWrapper() {
88         if (mPlaceholderWrapper != null) {
89             mPlaceholderWrapper.removeAllViews();
90         }
91         mPlaceholderWrapper.setVisibility(View.GONE);
92     }
93 
94     /**
95      * Removes the UI and calls the close listener.
96      */
close()97     public void close() {
98         removeOverlayAndHideWrapper();
99         if (mCloseListener != null) {
100             mCloseListener.onTutorialClosed();
101         }
102     }
103 }
104