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;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.accounts.OnAccountsUpdateListener;
22import android.content.Intent;
23import android.graphics.drawable.Drawable;
24import android.os.Bundle;
25
26import com.android.tv.settings.connectivity.ConnectivityListener;
27
28import java.util.Locale;
29
30/**
31 * Main settings which loads up the top level headers.
32 */
33public class MainSettings extends MenuActivity implements OnAccountsUpdateListener,
34        ConnectivityListener.Listener {
35
36    private static MainSettings sInstance;
37
38    private BrowseInfo mBrowseInfo;
39    private AccountManager mAccountManager;
40    private Locale mCurrentLocale;
41    private ConnectivityListener mConnectivityListener;
42
43    public static synchronized MainSettings getInstance() {
44        return sInstance;
45    }
46
47    @Override
48    protected void onCreate(Bundle savedInstanceState) {
49        mBrowseInfo = new BrowseInfo(this);
50        mBrowseInfo.init();
51        mCurrentLocale = Locale.getDefault();
52        mAccountManager = AccountManager.get(this);
53
54        super.onCreate(savedInstanceState);
55
56        mConnectivityListener = new ConnectivityListener(this, this);
57    }
58
59    @Override
60    protected void onStart() {
61        super.onStart();
62        mAccountManager.addOnAccountsUpdatedListener(this, null, true);
63        // Update here, just in case.
64        onAccountsUpdated(null);
65        mConnectivityListener.start();
66    }
67
68    @Override
69    protected void onResume() {
70        super.onResume();
71        sInstance = this;
72        updateAccessories();
73        mBrowseInfo.checkForDeveloperOptionUpdate();
74        mBrowseInfo.updateSounds();
75
76        // Update network item forcefully here
77        // because mBrowseInfo doesn't have information regarding Ethernet availability.
78        mBrowseInfo.updateWifi(mConnectivityListener.isEthernetAvailable());
79    }
80
81    @Override
82    protected void onPause() {
83        sInstance = null;
84        super.onPause();
85    }
86
87    @Override
88    protected void onStop() {
89        mAccountManager.removeOnAccountsUpdatedListener(this);
90        super.onStop();
91        mConnectivityListener.stop();
92    }
93
94    @Override
95    protected void onDestroy() {
96        super.onDestroy();
97        mAccountManager.removeOnAccountsUpdatedListener(this);
98    }
99
100    @Override
101    public void onConnectivityChange(Intent intent) {
102        mBrowseInfo.updateWifi(mConnectivityListener.isEthernetAvailable());
103    }
104
105    @Override
106    protected String getBrowseTitle() {
107        return getString(R.string.settings_app_name);
108    }
109
110    @Override
111    protected Drawable getBadgeImage() {
112        return getResources().getDrawable(R.drawable.ic_settings_app_icon);
113    }
114
115    @Override
116    protected BrowseInfoFactory getBrowseInfoFactory() {
117        if (!mCurrentLocale.equals(Locale.getDefault())) {
118            // the System Locale information has changed
119            mCurrentLocale = Locale.getDefault();
120            mBrowseInfo.rebuildInfo();
121        }
122
123        return mBrowseInfo;
124    }
125
126    @Override
127    public void onAccountsUpdated(Account[] accounts) {
128        mBrowseInfo.updateAccounts();
129    }
130
131    public void updateAccessories() {
132        mBrowseInfo.updateAccessories();
133    }
134}
135