Update To 11.40.268.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 import org.chromium.chrome.browser.profiles.ProfileAccountManagementMetrics;
15
16 /**
17  * Stub entry points and implementation interface for the account management screen.
18  */
19 public class AccountManagementScreenHelper {
20     private static AccountManagementScreenManager sManager;
21
22     /*
23      * TODO(guohui): add all Gaia service types.
24      * Enum for the Gaia service types, must match GAIAServiceType in
25      * signin_header_helper.h
26      */
27     public static final int GAIA_SERVICE_TYPE_SIGNUP = 5;
28
29     private static final String EXTRA_ACCOUNT_TYPES = "account_types";
30     private static final String EXTRA_VALUE_GOOGLE_ACCOUNTS = "com.google";
31
32     /**
33      * The screen manager interface.
34      */
35     public interface AccountManagementScreenManager {
36         /**
37          * Opens the account management UI screen.
38          * @param applicationContext The application context.
39          * @param profile The user profile.
40          * @param gaiaServiceType A signin::GAIAServiceType value that triggered the dialog.
41          */
42         void openAccountManagementScreen(
43                 Context applicationContext, Profile profile, int gaiaServiceType);
44     }
45
46     /**
47      * Sets the screen manager.
48      * @param manager An implementation of AccountManagementScreenManager interface.
49      */
50     public static void setManager(AccountManagementScreenManager manager) {
51         ThreadUtils.assertOnUiThread();
52         sManager = manager;
53     }
54
55     @CalledByNative
56     private static void openAccountManagementScreen(
57             Context applicationContext, Profile profile, int gaiaServiceType) {
58         ThreadUtils.assertOnUiThread();
59         if (sManager == null) return;
60
61         if (gaiaServiceType == GAIA_SERVICE_TYPE_SIGNUP)
62             openAndroidAccountCreationScreen(applicationContext);
63         else
64             sManager.openAccountManagementScreen(applicationContext, profile, gaiaServiceType);
65     }
66
67     /**
68      * Opens the Android account manager for adding or creating a Google account.
69      * @param applicationContext
70      */
71     private static void openAndroidAccountCreationScreen(
72             Context applicationContext) {
73         logEvent(ProfileAccountManagementMetrics.DIRECT_ADD_ACCOUNT, GAIA_SERVICE_TYPE_SIGNUP);
74
75         Intent createAccountIntent = new Intent(Settings.ACTION_ADD_ACCOUNT);
76         createAccountIntent.putExtra(
77                 EXTRA_ACCOUNT_TYPES, new String[]{EXTRA_VALUE_GOOGLE_ACCOUNTS});
78         createAccountIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
79                 | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
80                 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
81
82         applicationContext.startActivity(createAccountIntent);
83     }
84
85     /**
86      * Log a UMA event for a given metric and a signin type.
87      * @param metric One of ProfileAccountManagementMetrics constants.
88      * @param gaiaServiceType A signin::GAIAServiceType.
89      */
90     public static void logEvent(int metric, int gaiaServiceType) {
91         nativeLogEvent(metric, gaiaServiceType);
92     }
93
94     // Native methods.
95     private static native void nativeLogEvent(int metric, int gaiaServiceType);
96 }