SliceBroadcastReceiver.java revision 8b099f294dac82878af162e6b5842413abbee2a6
1/* 2 * Copyright 2017 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.example.androidx.slice.demos; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.net.wifi.WifiManager; 23import android.os.Handler; 24 25import androidx.app.slice.core.SliceHints; 26 27/** 28 * Responds to actions performed on slices and notifies slices of updates in state changes. 29 */ 30public class SliceBroadcastReceiver extends BroadcastReceiver { 31 32 @Override 33 public void onReceive(Context context, Intent i) { 34 String action = i.getAction(); 35 switch (action) { 36 case SampleSliceProvider.ACTION_WIFI_CHANGED: 37 WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 38 boolean newState = i.getBooleanExtra(SliceHints.EXTRA_TOGGLE_STATE, 39 wm.isWifiEnabled()); 40 wm.setWifiEnabled(newState); 41 // Wait a bit for wifi to update (TODO: is there a better way to do this?) 42 Handler h = new Handler(); 43 h.postDelayed(() -> { 44 context.getContentResolver().notifyChange(SampleSliceProvider.WIFI, null); 45 }, 1000); 46 break; 47 } 48 } 49} 50