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