summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/app/FirstRunDialog.java
blob: 35330fa85a6f49539d349b79d2a3f550898122fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.app;

import android.Manifest;
import android.app.Dialog;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.view.ViewGroup;

import com.android.camera.device.CameraId;
import com.android.camera.exif.Rational;
import com.android.camera.one.OneCamera.Facing;
import com.android.camera.one.OneCameraAccessException;
import com.android.camera.one.OneCameraManager;
import com.android.camera.settings.Keys;
import com.android.camera.settings.ResolutionSetting;
import com.android.camera.settings.ResolutionUtil;
import com.android.camera.settings.SettingsManager;
import com.android.camera.util.ApiHelper;
import com.android.camera.widget.AspectRatioDialogLayout;
import com.android.camera.widget.LocationDialogLayout;

/**
 * The dialog to show when users open the app for the first time.
 */
public class FirstRunDialog {

    public interface FirstRunDialogListener {
        public void onFirstRunStateReady();
        public void onFirstRunDialogCancelled();
        public void onCameraAccessException();
    }

    /** The default preference of aspect ratio. */
    private static final Rational DEFAULT_ASPECT_RATIO = ResolutionUtil.ASPECT_RATIO_4x3;

    /** The default preference of whether enabling location recording. */
    private static final boolean DEFAULT_LOCATION_RECORDING_ENABLED = true;

    /** Request code to PackageManager */
    private static final int PERMISSION_REQUEST_CODE = 1;

    /** Listener to receive events. */
    private final FirstRunDialogListener mListener;

    /** The app controller. */
    private final AppController mAppController;

    /** The hardware manager. */
    private final OneCameraManager mOneCameraManager;

    /** The activity context. */
    private final Context mContext;

    /** The resolution settings. */
    private final ResolutionSetting mResolutionSetting;

    /** The settings manager. */
    private final SettingsManager mSettingsManager;

    /** Aspect ratio preference dialog */
    private Dialog mAspectRatioPreferenceDialog;

    /** Location preference dialog */
    private Dialog mLocationPreferenceDialog;

    /**
     * Constructs a first run dialog.
     *
     */
    public FirstRunDialog(
          AppController appController,
          Context activityContext,
          ResolutionSetting resolutionSetting,
          SettingsManager settingManager,
          OneCameraManager hardwareManager,
          FirstRunDialogListener listener) {
        mAppController = appController;
        mContext = activityContext;
        mResolutionSetting = resolutionSetting;
        mSettingsManager = settingManager;
        mOneCameraManager = hardwareManager;
        mListener = listener;
    }

    /**
     * Shows first run dialogs if necessary.
     */
    public void showIfNecessary() {
        if (shouldShowLocationDialog()) {
            // When people open the app for the first time, prompt two dialogs to
            // ask preferences about location and aspect ratio. The first dialog is
            // location reference.
            promptLocationPreferenceDialog();
        } else if (shouldShowAspectRatioDialog()) {
            /**
             * If people already set location preference, prompt aspect ratio dialog.
             */
            promptAspectRatioPreferenceDialog();
        } else {
            mListener.onFirstRunStateReady();
        }
    }

    /**
     * Dismiss all shown dialogs.
     */
    public void dismiss() {
        if (mAspectRatioPreferenceDialog != null) {
            // Remove the listener since we actively dismiss the dialog.
            mAspectRatioPreferenceDialog.setOnDismissListener(null);
            mAspectRatioPreferenceDialog.dismiss();
            mAspectRatioPreferenceDialog = null;
        }
        if (mLocationPreferenceDialog != null) {
            // Remove the listener since we actively dismiss the dialog.
            mLocationPreferenceDialog.setOnDismissListener(null);
            mLocationPreferenceDialog.dismiss();
            mLocationPreferenceDialog = null;
        }
    }

    /**
     * Whether first run dialogs should be presented to the user.
     *
     * @return Whether first run dialogs should be presented to the user.
     */
    private boolean shouldShowLocationDialog() {
        return !mSettingsManager.isSet(SettingsManager.SCOPE_GLOBAL, Keys.KEY_RECORD_LOCATION);
    }

    private boolean shouldShowAspectRatioDialog() {
        return mAppController.getCameraAppUI().shouldShowAspectRatioDialog();
    }

    /**
     * Prompts a dialog to allow people to choose aspect ratio preference when
     * people open the app for the first time. If the preference has been set,
     * this will return false.
     */
    private void promptAspectRatioPreferenceDialog() {
        // Create a content view for the dialog.
        final AspectRatioDialogLayout dialogLayout = new AspectRatioDialogLayout(
                mContext, DEFAULT_ASPECT_RATIO);
        dialogLayout.setListener(new AspectRatioDialogLayout.AspectRatioDialogListener() {
            @Override
            public void onConfirm(Rational aspectRatio) {
                // Change resolution setting based on the chosen aspect ratio.
                try {
                    CameraId backCameraId = mOneCameraManager.findFirstCameraFacing(Facing.BACK);
                    if (backCameraId != null) {
                        mResolutionSetting.setPictureAspectRatio(backCameraId, aspectRatio);
                    }
                    CameraId frontCameraId = mOneCameraManager.findFirstCameraFacing(Facing.FRONT);
                    if (frontCameraId != null) {
                        mResolutionSetting.setPictureAspectRatio(frontCameraId, aspectRatio);
                    }
                } catch (OneCameraAccessException ex) {
                    mListener.onCameraAccessException();
                    return;
                }

                // Mark that user has made the choice.
                mSettingsManager.set(
                        SettingsManager.SCOPE_GLOBAL,
                        Keys.KEY_USER_SELECTED_ASPECT_RATIO,
                        true);

                // Dismiss all dialogs.
                dismiss();

                // Notify that the app is ready to go.
                mListener.onFirstRunStateReady();
            }
        });

        // Create the dialog.
        mAspectRatioPreferenceDialog = mAppController.createDialog();
        mAspectRatioPreferenceDialog.setContentView(dialogLayout, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        // Detect if the dialog is dismissed by back button.
        mAspectRatioPreferenceDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mAspectRatioPreferenceDialog = null;
                dismiss();
                mListener.onFirstRunDialogCancelled();
            }
        });

        // Show the dialog.
        mAspectRatioPreferenceDialog.show();
    }

    private void checkLocationPermission() {
        if (mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            Activity activity = (Activity) mContext;
            activity.requestPermissions(
                new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSION_REQUEST_CODE);
            mSettingsManager.set(SettingsManager.SCOPE_GLOBAL,
                Keys.KEY_HAS_SEEN_PERMISSIONS_DIALOGS, true);
        }
    }

    /**
     * Prompts a dialog to allow people to choose location preference when
     * people open the app for the first time. If the preference has been set,
     * this will return false.
     */
    private void promptLocationPreferenceDialog() {
        // Create a content view for the dialog.
        final LocationDialogLayout dialogLayout = new LocationDialogLayout(
                mContext, DEFAULT_LOCATION_RECORDING_ENABLED);
        dialogLayout.setListener(new LocationDialogLayout.LocationDialogListener() {
            @Override
            public void onConfirm(boolean locationRecordingEnabled) {
                // Change the location preference setting.
                mSettingsManager.set(
                        SettingsManager.SCOPE_GLOBAL,
                        Keys.KEY_RECORD_LOCATION,
                        locationRecordingEnabled);
                if (locationRecordingEnabled) {
                    checkLocationPermission();
                }

                if (shouldShowAspectRatioDialog()) {
                    // Prompt the second dialog about aspect ratio preference.
                    promptAspectRatioPreferenceDialog();
                } else {
                    // Dismiss all dialogs.
                    dismiss();
                    // Notify that the app is ready to go.
                    mListener.onFirstRunStateReady();
                }
            }
        });

        // Create the dialog.
        mLocationPreferenceDialog = mAppController.createDialog();
        mLocationPreferenceDialog.setContentView(dialogLayout, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        // Detect if the dialog is dismissed by back button.
        mLocationPreferenceDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mLocationPreferenceDialog = null;
                dismiss();
                mListener.onFirstRunDialogCancelled();
            }
        });

        // Show the dialog.
        mLocationPreferenceDialog.show();
    }
}