1 /* 2 * Copyright (C) 2016 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.settings.dashboard; 18 19 import android.accounts.Account; 20 import android.accounts.AccountManager; 21 import android.accounts.OnAccountsUpdateListener; 22 import android.app.Activity; 23 import android.content.Context; 24 import android.net.ConnectivityManager; 25 import android.net.Network; 26 import android.net.NetworkCapabilities; 27 import android.net.NetworkInfo; 28 import android.net.NetworkRequest; 29 import android.os.Bundle; 30 import android.os.Handler; 31 import android.os.Looper; 32 import android.support.v7.widget.LinearLayoutManager; 33 import android.support.v7.widget.RecyclerView; 34 import android.view.LayoutInflater; 35 import android.view.View; 36 import android.view.ViewGroup; 37 38 import com.android.internal.logging.nano.MetricsProto; 39 import com.android.settings.R; 40 import com.android.settings.core.InstrumentedFragment; 41 import com.android.settings.overlay.FeatureFactory; 42 import com.android.settings.overlay.SupportFeatureProvider; 43 44 /** 45 * Fragment for support tab in SettingsGoogle. 46 */ 47 public final class SupportFragment extends InstrumentedFragment implements View.OnClickListener, 48 OnAccountsUpdateListener { 49 50 private final Handler mHandler = new Handler(Looper.getMainLooper()); 51 private final ConnectivityManager.NetworkCallback mNetworkCallback = 52 new ConnectivityManager.NetworkCallback() { 53 54 @Override 55 public void onCapabilitiesChanged(Network network, 56 NetworkCapabilities capabilities) { 57 postConnectivityChanged(); 58 } 59 60 @Override 61 public void onAvailable(Network network) { 62 postConnectivityChanged(); 63 } 64 65 @Override 66 public void onLost(Network network) { 67 postConnectivityChanged(); 68 } 69 }; 70 71 private Activity mActivity; 72 private View mContent; 73 private RecyclerView mRecyclerView; 74 private SupportItemAdapter mSupportItemAdapter; 75 private AccountManager mAccountManager; 76 private SupportFeatureProvider mSupportFeatureProvider; 77 private ConnectivityManager mConnectivityManager; 78 79 @Override 80 public int getMetricsCategory() { 81 return MetricsProto.MetricsEvent.SUPPORT_FRAGMENT; 82 } 83 84 @Override 85 public void onCreate(Bundle savedInstanceState) { 86 super.onCreate(savedInstanceState); 87 setHasOptionsMenu(true); 88 mActivity = getActivity(); 89 mAccountManager = AccountManager.get(mActivity); 90 mSupportFeatureProvider = 91 FeatureFactory.getFactory(mActivity).getSupportFeatureProvider(mActivity); 92 mSupportItemAdapter = new SupportItemAdapter(mActivity, savedInstanceState, 93 mSupportFeatureProvider, mMetricsFeatureProvider, this /* itemClickListener */); 94 mConnectivityManager = 95 (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE); 96 } 97 98 @Override 99 public View onCreateView(LayoutInflater inflater, ViewGroup container, 100 Bundle savedInstanceState) { 101 mContent = inflater.inflate(R.layout.support_fragment, container, false); 102 mRecyclerView = (RecyclerView) mContent.findViewById(R.id.support_items); 103 mRecyclerView.setLayoutManager(new LinearLayoutManager( 104 getActivity(), LinearLayoutManager.VERTICAL, false /* reverseLayout */)); 105 mRecyclerView.setAdapter(mSupportItemAdapter); 106 return mContent; 107 } 108 109 @Override 110 public void onResume() { 111 super.onResume(); 112 // Monitor account change. 113 mAccountManager.addOnAccountsUpdatedListener( 114 this /* listener */, null /* handler */, true /* updateImmediately */); 115 // Monitor connectivity 116 mConnectivityManager.registerNetworkCallback( 117 new NetworkRequest.Builder() 118 .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) 119 .build(), 120 mNetworkCallback); 121 mSupportItemAdapter.setHasInternet(hasInternet()); 122 mSupportItemAdapter.refreshData(); 123 } 124 125 @Override 126 public void onPause() { 127 super.onPause(); 128 // Stop monitor account change. 129 mAccountManager.removeOnAccountsUpdatedListener(this /* listener */); 130 // Stop monitor connectivity. 131 mConnectivityManager.unregisterNetworkCallback(mNetworkCallback); 132 } 133 134 @Override 135 public void onSaveInstanceState(Bundle outState) { 136 super.onSaveInstanceState(outState); 137 mSupportItemAdapter.onSaveInstanceState(outState); 138 } 139 140 @Override 141 public void onAccountsUpdated(Account[] accounts) { 142 // Account changed, update support items. 143 mSupportItemAdapter.setAccounts( 144 mSupportFeatureProvider.getSupportEligibleAccounts(mActivity)); 145 } 146 147 @Override 148 public void onClick(View v) { 149 final SupportItemAdapter.ViewHolder vh = 150 (SupportItemAdapter.ViewHolder) mRecyclerView.getChildViewHolder(v); 151 mSupportItemAdapter.onItemClicked(vh.getAdapterPosition()); 152 } 153 154 private void postConnectivityChanged() { 155 mHandler.post(new Runnable() { 156 @Override 157 public void run() { 158 if (mSupportItemAdapter != null) { 159 mSupportItemAdapter.setHasInternet(hasInternet()); 160 } 161 } 162 }); 163 } 164 165 private boolean hasInternet() { 166 final NetworkInfo activeNetwork = mConnectivityManager.getActiveNetworkInfo(); 167 return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); 168 } 169 } 170