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.
5 package org.chromium.chrome.browser.profiles;
7 import android.graphics.Bitmap;
9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.ObserverList;
11 import org.chromium.base.ThreadUtils;
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.
18 public class ProfileDownloader {
19 private static final ObserverList<Observer> sObservers = new ObserverList<Observer>();
22 * Interface for receiving notifications on account information updates.
24 public interface Observer {
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.
32 void onProfileDownloaded(String accountId, String fullName, String givenName,
38 * @param observer An observer.
40 public static void addObserver(Observer observer) {
41 sObservers.addObserver(observer);
46 * @param observer An observer.
48 public static void removeObserver(Observer observer) {
49 sObservers.removeObserver(observer);
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)
58 public static void startFetchingAccountInfoFor(
59 Profile profile, String accountId, int imageSidePixels) {
60 ThreadUtils.assertOnUiThread();
61 nativeStartFetchingAccountInfoFor(profile, accountId, imageSidePixels);
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);
74 * @param profile Profile
75 * @return The profile full name if cached, or null.
77 public static String getCachedFullName(Profile profile) {
78 return nativeGetCachedFullNameForPrimaryAccount(profile);
82 * @param profile Profile
83 * @return The profile given name if cached, or null.
85 public static String getCachedGivenName(Profile profile) {
86 return nativeGetCachedGivenNameForPrimaryAccount(profile);
90 * @param profile Profile
91 * @return The profile avatar if cached, or null.
93 public static Bitmap getCachedAvatar(Profile profile) {
94 return nativeGetCachedAvatarForPrimaryAccount(profile);
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);