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