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
17package com.android.camera.ui;
18
19import android.view.LayoutInflater;
20import android.view.View;
21import android.view.ViewGroup;
22
23import 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 */
29public 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         */
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     */
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     */
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     */
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     */
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     */
97    public void close() {
98        removeOverlayAndHideWrapper();
99        if (mCloseListener != null) {
100            mCloseListener.onTutorialClosed();
101        }
102    }
103}
104