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
5package org.chromium.chrome.browser.profiles;
6
7import android.graphics.Bitmap;
8
9import org.chromium.base.CalledByNative;
10import org.chromium.base.ObserverList;
11import 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 */
18public 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 bitmap A user picture.
30         */
31        void onProfileDownloaded(String accountId, String fullName, Bitmap bitmap);
32    }
33
34    /**
35     * Add an observer.
36     * @param observer An observer.
37     */
38    public static void addObserver(Observer observer) {
39        sObservers.addObserver(observer);
40    }
41
42    /**
43     * Remove an observer.
44     * @param observer An observer.
45     */
46    public static void removeObserver(Observer observer) {
47        sObservers.removeObserver(observer);
48    }
49
50    /**
51     * Starts fetching the account information for a given account.
52     * @param profile Profile associated with the request
53     * @param accountId Account name to fetch the information for
54     * @param imageSidePixels Request image side (in pixels)
55     */
56    public static void startFetchingAccountInfoFor(
57            Profile profile, String accountId, int imageSidePixels) {
58        ThreadUtils.assertOnUiThread();
59        nativeStartFetchingAccountInfoFor(profile, accountId, imageSidePixels);
60    }
61
62    @CalledByNative
63    private static void onProfileDownloadSuccess(String accountId, String fullName, Bitmap bitmap) {
64        ThreadUtils.assertOnUiThread();
65        for (Observer observer : sObservers) {
66            observer.onProfileDownloaded(accountId, fullName, bitmap);
67        }
68    }
69
70    /**
71     * @param profile Profile
72     * @return The profile name if cached, or null.
73     */
74    public static String getCachedName(Profile profile) {
75        return nativeGetCachedNameForPrimaryAccount(profile);
76    }
77
78    /**
79     * @param profile Profile
80     * @return The profile avatar if cached, or null.
81     */
82    public static Bitmap getCachedAvatar(Profile profile) {
83        return nativeGetCachedAvatarForPrimaryAccount(profile);
84    }
85
86    // Native methods.
87    private static native void nativeStartFetchingAccountInfoFor(
88            Profile profile, String accountId, int imageSidePixels);
89    private static native String nativeGetCachedNameForPrimaryAccount(Profile profile);
90    private static native Bitmap nativeGetCachedAvatarForPrimaryAccount(Profile profile);
91}
92