• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.connectivity;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 
24 /**
25  * The {@link BroadcastReceiver} for detecting and recording ethernet connections
26  */
27 public class EthernetDetectionReceiver extends BroadcastReceiver {
28     @Override
onReceive(Context context, Intent intent)29     public void onReceive(Context context, Intent intent) {
30         if (!NetworkChangeDetectionConfigs.ACTION_ETHERNET_DETECTED.equals(intent.getAction())) {
31             return;
32         }
33         SharedPreferences networkChangePref = context.getSharedPreferences(
34                 NetworkChangeDetectionConfigs.SHARED_PREFERENCES_NAME,
35                 Context.MODE_PRIVATE);
36         boolean isEthernetRecorded = networkChangePref.getBoolean(
37                 NetworkChangeDetectionConfigs.ETHERNET_RECORD_PREFERENCE_KEY, false);
38 
39         if (!isEthernetRecorded) {
40             SharedPreferences.Editor networkChangePrefEditor = networkChangePref.edit();
41             int currentCount = networkChangePref.getInt(
42                     NetworkChangeDetectionConfigs.PREFERENCE_KEY, 0);
43             networkChangePrefEditor.putInt(NetworkChangeDetectionConfigs.PREFERENCE_KEY,
44                     currentCount + 1);
45             networkChangePrefEditor.putBoolean(
46                     NetworkChangeDetectionConfigs.ETHERNET_RECORD_PREFERENCE_KEY, true);
47             networkChangePrefEditor.commit();
48 
49             // Notify NetworkChange observers of change.
50             context.getContentResolver().notifyChange(
51                     NetworkChangeDetectionConfigs.CONTENT_URI, null);
52         }
53     }
54 }
55