Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / profiles / ProfileDownloader.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.profiles;
6
7 import android.graphics.Bitmap;
8
9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.ObserverList;
11 import org.chromium.base.ThreadUtils;
12
13 /**
14  * Android wrapper of the ProfileDownloader which provides access from the Java layer.
15  * The native ProfileDownloader requires its access to be in the UI thread.
16  * See chrome/browser/profiles/profile_downloader.h/cc for more details.
17  */
18 public class ProfileDownloader {
19     private static final ObserverList<Observer> sObservers = new ObserverList<Observer>();
20
21     /**
22      * Interface for receiving notifications on account information updates.
23      */
24     public interface Observer {
25         /**
26          * Notifies that an account data in the profile has been updated.
27          * @param accountId An account ID.
28          * @param fullName A full name.
29          * @param givenName A given name.
30          * @param bitmap A user picture.
31          */
32         void onProfileDownloaded(String accountId, String fullName, String givenName,
33                 Bitmap bitmap);
34     }
35
36     /**
37      * Add an observer.
38      * @param observer An observer.
39      */
40     public static void addObserver(Observer observer) {
41         sObservers.addObserver(observer);
42     }
43
44     /**
45      * Remove an observer.
46      * @param observer An observer.
47      */
48     public static void removeObserver(Observer observer) {
49         sObservers.removeObserver(observer);
50     }
51
52     /**
53      * Starts fetching the account information for a given account.
54      * @param profile Profile associated with the request
55      * @param accountId Account name to fetch the information for
56      * @param imageSidePixels Request image side (in pixels)
57      */
58     public static void startFetchingAccountInfoFor(
59             Profile profile, String accountId, int imageSidePixels) {
60         ThreadUtils.assertOnUiThread();
61         nativeStartFetchingAccountInfoFor(profile, accountId, imageSidePixels);
62     }
63
64     @CalledByNative
65     private static void onProfileDownloadSuccess(String accountId, String fullName,
66             String givenName, Bitmap bitmap) {
67         ThreadUtils.assertOnUiThread();
68         for (Observer observer : sObservers) {
69             observer.onProfileDownloaded(accountId, fullName, givenName, bitmap);
70         }
71     }
72
73     /**
74      * @param profile Profile
75      * @return The profile full name if cached, or null.
76      */
77     public static String getCachedFullName(Profile profile) {
78         return nativeGetCachedFullNameForPrimaryAccount(profile);
79     }
80
81     /**
82      * @param profile Profile
83      * @return The profile given name if cached, or null.
84      */
85     public static String getCachedGivenName(Profile profile) {
86         return nativeGetCachedGivenNameForPrimaryAccount(profile);
87     }
88
89     /**
90      * @param profile Profile
91      * @return The profile avatar if cached, or null.
92      */
93     public static Bitmap getCachedAvatar(Profile profile) {
94         return nativeGetCachedAvatarForPrimaryAccount(profile);
95     }
96
97     // Native methods.
98     private static native void nativeStartFetchingAccountInfoFor(
99             Profile profile, String accountId, int imageSidePixels);
100     private static native String nativeGetCachedFullNameForPrimaryAccount(Profile profile);
101     private static native String nativeGetCachedGivenNameForPrimaryAccount(Profile profile);
102     private static native Bitmap nativeGetCachedAvatarForPrimaryAccount(Profile profile);
103 }