1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tv.settings.accounts;
18 
19 import com.android.tv.settings.MenuItem;
20 import com.android.tv.settings.util.UriUtils;
21 import com.android.tv.settings.util.AccountImageHelper;
22 
23 import android.accounts.Account;
24 import android.content.Context;
25 
26 import android.net.Uri;
27 
28 /**
29  * Gets a URI to the account picture.
30  */
31 public class AccountImageUriGetter implements MenuItem.UriGetter {
32 
33     private final Context mContext;
34     private final Account mAccount;
35     private final Uri mNotifyUri;
36     private String mIconUri;
37 
AccountImageUriGetter(Context context, Account account)38     public AccountImageUriGetter(Context context, Account account) {
39         this(context, account, null);
40     }
41 
AccountImageUriGetter(Context context, Account account, Uri changeNotifyUri)42     public AccountImageUriGetter(Context context, Account account, Uri changeNotifyUri) {
43         mContext = context;
44         mAccount = account;
45         mNotifyUri = changeNotifyUri;
46     }
47 
48     @Override
getUri()49     public String getUri() {
50         if (mIconUri != null) {
51             return mIconUri;
52         }
53 
54         if (mAccount != null) {
55             mIconUri = UriUtils.getAccountImageUri(mAccount.name, mNotifyUri).toString();
56             if (mIconUri != null) {
57                 return mIconUri;
58             }
59         }
60 
61         // Return this but don't save into mIconUri, requery later.
62         return AccountImageHelper.getDefaultPictureUri(mContext);
63     }
64 }
65