Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / signin / AccountManagementScreenHelper.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.signin;
6
7 import android.content.Context;
8 import android.content.Intent;
9 import android.provider.Settings;
10
11 import org.chromium.base.CalledByNative;
12 import org.chromium.base.ThreadUtils;
13 import org.chromium.chrome.browser.profiles.Profile;
14
15 /**
16  * Stub entry points and implementation interface for the account management screen.
17  */
18 public class AccountManagementScreenHelper {
19     private static AccountManagementScreenManager sManager;
20
21     /*
22      * TODO(aruslan): http://crbug.com/379987 Move to a generator.
23      * Enum for tracking user interactions with the account management menu
24      * on Android.
25      * This must match enum ProfileAndroidAccountManagementMenu in profile_metrics.h.
26      */
27     // User arrived at the Account management screen.
28     public static final int ACCOUNT_MANAGEMENT_MENU_VIEW = 0;
29     // User arrived at the Account management screen, and clicked Add account.
30     public static final int ACCOUNT_MANAGEMENT_MENU_ADD_ACCOUNT = 1;
31     // User arrived at the Account management screen, and clicked Go incognito.
32     public static final int ACCOUNT_MANAGEMENT_MENU_GO_INCOGNITO = 2;
33     // User arrived at the Account management screen, and clicked on primary.
34     public static final int ACCOUNT_MANAGEMENT_MENU_CLICK_PRIMARY_ACCOUNT = 3;
35     // User arrived at the Account management screen, and clicked on secondary.
36     public static final int ACCOUNT_MANAGEMENT_MENU_CLICK_SECONDARY_ACCOUNT = 4;
37     // User arrived at the Account management screen, toggled Chrome signout.
38     public static final int ACCOUNT_MANAGEMENT_MENU_TOGGLE_SIGNOUT = 5;
39     // User toggled Chrome signout, and clicked Signout.
40     public static final int ACCOUNT_MANAGEMENT_MENU_SIGNOUT_SIGNOUT = 6;
41     // User toggled Chrome signout, and clicked Cancel.
42     public static final int ACCOUNT_MANAGEMENT_MENU_SIGNOUT_CANCEL = 7;
43     // User arrived at the android Account management screen directly from some
44     // Gaia requests.
45     public static final int ACCOUNT_MANAGEMENT_ADD_ACCOUNT = 8;
46     /*
47      * TODO(guohui): add all Gaia service types.
48      * Enum for the Gaia service types, must match GAIAServiceType in
49      * signin_header_helper.h
50      */
51     public static final int GAIA_SERVICE_TYPE_SIGNUP = 5;
52
53     private static final String EXTRA_ACCOUNT_TYPES = "account_types";
54     private static final String EXTRA_VALUE_GOOGLE_ACCOUNTS = "com.google";
55
56     /**
57      * The screen manager interface.
58      */
59     public interface AccountManagementScreenManager {
60         /**
61          * Opens the account management UI screen.
62          * @param applicationContext The application context.
63          * @param profile The user profile.
64          * @param gaiaServiceType A signin::GAIAServiceType value that triggered the dialog.
65          */
66         void openAccountManagementScreen(
67                 Context applicationContext, Profile profile, int gaiaServiceType);
68     }
69
70     /**
71      * Sets the screen manager.
72      * @param manager An implementation of AccountManagementScreenManager interface.
73      */
74     public static void setManager(AccountManagementScreenManager manager) {
75         ThreadUtils.assertOnUiThread();
76         sManager = manager;
77     }
78
79     @CalledByNative
80     private static void openAccountManagementScreen(
81             Context applicationContext, Profile profile, int gaiaServiceType) {
82         ThreadUtils.assertOnUiThread();
83         if (sManager == null) return;
84
85         if (gaiaServiceType == GAIA_SERVICE_TYPE_SIGNUP)
86             openAndroidAccountCreationScreen(applicationContext);
87         else
88             sManager.openAccountManagementScreen(applicationContext, profile, gaiaServiceType);
89     }
90
91     /**
92      * Opens the Android account manager for adding or creating a Google account.
93      * @param applicationContext
94      */
95     private static void openAndroidAccountCreationScreen(
96             Context applicationContext) {
97         logEvent(ACCOUNT_MANAGEMENT_ADD_ACCOUNT, GAIA_SERVICE_TYPE_SIGNUP);
98
99         Intent createAccountIntent = new Intent(Settings.ACTION_ADD_ACCOUNT);
100         createAccountIntent.putExtra(
101                 EXTRA_ACCOUNT_TYPES, new String[]{EXTRA_VALUE_GOOGLE_ACCOUNTS});
102         createAccountIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
103                 | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
104                 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
105
106         applicationContext.startActivity(createAccountIntent);
107     }
108
109     /**
110      * Log a UMA event for a given metric and a signin type.
111      * @param metric A PROFILE_ANDROID_ACCOUNT_MANAGEMENT_MENU metric.
112      * @param gaiaServiceType A signin::GAIAServiceType.
113      */
114     public static void logEvent(int metric, int gaiaServiceType) {
115         nativeLogEvent(metric, gaiaServiceType);
116     }
117
118     // Native methods.
119     private static native void nativeLogEvent(int metric, int gaiaServiceType);
120 }