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
17package com.android.tv.settings.accounts;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.graphics.Bitmap;
22import android.graphics.drawable.BitmapDrawable;
23import android.graphics.drawable.Drawable;
24import android.net.Uri;
25import android.os.Bundle;
26
27import com.android.tv.settings.BrowseInfoFactory;
28import com.android.tv.settings.MenuActivity;
29import com.android.tv.settings.util.UriUtils;
30import com.android.tv.settings.widget.BitmapDownloader;
31import com.android.tv.settings.widget.BitmapDownloader.BitmapCallback;
32import com.android.tv.settings.widget.BitmapWorkerOptions;
33
34public class AccountSettingsActivity extends MenuActivity {
35
36    public static final String EXTRA_ACCOUNT = "account_name";
37
38    private Drawable mAccountDrawable;
39
40    @Override
41    protected String getBrowseTitle() {
42        return getIntent().getStringExtra(EXTRA_ACCOUNT);
43    }
44
45    @Override
46    protected Drawable getBadgeImage() {
47        return mAccountDrawable;
48    }
49
50    @Override
51    protected BrowseInfoFactory getBrowseInfoFactory() {
52        return new AccountsBrowseInfo(this, getIntent().getStringExtra(EXTRA_ACCOUNT));
53    }
54
55    @Override
56    protected void onCreate(Bundle savedInstanceState) {
57        super.onCreate(savedInstanceState);
58        Uri imageUri = UriUtils.getAccountImageUri(getIntent().getStringExtra(EXTRA_ACCOUNT), null);
59        BitmapCallback bitmapCallBack = new BitmapCallback() {
60            @Override
61            public void onBitmapRetrieved(Bitmap bitmap) {
62                if (bitmap != null) {
63                    mAccountDrawable = new BitmapDrawable(getResources(), bitmap);
64                    updateBrowseParams();
65                }
66            }
67        };
68
69        BitmapWorkerOptions bitmapWorkerOptions = new BitmapWorkerOptions.Builder(this)
70                .resource(imageUri).build();
71        BitmapDownloader.getInstance(this).getBitmap(bitmapWorkerOptions, bitmapCallBack);
72    }
73
74    @Override
75    protected void onResume() {
76        super.onResume();
77        String accountName = getIntent().getStringExtra(EXTRA_ACCOUNT);
78        for (Account account : AccountManager.get(this).getAccounts()) {
79            if (account.name.equals(accountName)) {
80                return;
81            }
82        }
83        setResult(RESULT_CANCELED);
84        finish();
85    }
86
87}
88