OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.ntp; |
| 6 |
| 7 import org.chromium.base.annotations.CalledByNative; |
| 8 import org.chromium.chrome.browser.profiles.Profile; |
| 9 |
| 10 /** |
| 11 * Retrieve the user's interests. |
| 12 */ |
| 13 public class InterestsService { |
| 14 private long mNativeInterestsService; |
| 15 |
| 16 /** |
| 17 * A user's interest. |
| 18 */ |
| 19 public static class Interest { |
| 20 private final String mName; |
| 21 private final String mImageUrl; |
| 22 private final double mRelevance; |
| 23 |
| 24 public Interest(String name, String imageUrl, double relevance) { |
| 25 this.mName = name; |
| 26 this.mImageUrl = imageUrl; |
| 27 this.mRelevance = relevance; |
| 28 } |
| 29 |
| 30 public String getName() { |
| 31 return mName; |
| 32 } |
| 33 |
| 34 public String getImageUrl() { |
| 35 return mImageUrl; |
| 36 } |
| 37 |
| 38 public double getRelevance() { |
| 39 return mRelevance; |
| 40 } |
| 41 } |
| 42 /** |
| 43 * Interface for receiving the interests of a user. |
| 44 */ |
| 45 public interface GetInterestsCallback { |
| 46 /** |
| 47 * Callback method for fetching the interests of a user. |
| 48 * In case of an error an empty array will be returned. |
| 49 * |
| 50 * @param interests The array of interests. |
| 51 */ |
| 52 @CalledByNative("GetInterestsCallback") |
| 53 public void onInterestsAvailableCallback(Interest[] interests); |
| 54 } |
| 55 |
| 56 /** |
| 57 * InterestsService constructor requires a valid user profile object. |
| 58 * |
| 59 * @param profile The profile for which to fetch the interests |
| 60 */ |
| 61 public InterestsService(Profile profile) { |
| 62 mNativeInterestsService = nativeInit(profile); |
| 63 } |
| 64 |
| 65 /** |
| 66 * Cleans up the C++ side of this class. This instance must not be used afte
r calling destroy(). |
| 67 */ |
| 68 public void destroy() { |
| 69 assert mNativeInterestsService != 0; |
| 70 nativeDestroy(mNativeInterestsService); |
| 71 mNativeInterestsService = 0; |
| 72 } |
| 73 |
| 74 public void getInterests(final GetInterestsCallback callback) { |
| 75 GetInterestsCallback wrappedCallback = new GetInterestsCallback() { |
| 76 @Override |
| 77 public void onInterestsAvailableCallback(Interest[] interests) { |
| 78 // Don't notify callback if we've already been destroyed. |
| 79 if (mNativeInterestsService != 0) { |
| 80 callback.onInterestsAvailableCallback(interests); |
| 81 } |
| 82 } |
| 83 }; |
| 84 |
| 85 nativeGetInterests(mNativeInterestsService, wrappedCallback); |
| 86 } |
| 87 |
| 88 /* |
| 89 * Helper method for the native part. |
| 90 */ |
| 91 @CalledByNative |
| 92 private static Interest createInterest(String name, String imageUrl, double
relevance) { |
| 93 return new Interest(name, imageUrl, relevance); |
| 94 } |
| 95 |
| 96 private native long nativeInit(Profile profile); |
| 97 private native void nativeDestroy(long nativeInterestsService); |
| 98 private native void nativeGetInterests( |
| 99 long nativeInterestsService, GetInterestsCallback callback); |
| 100 } |
OLD | NEW |