Mady Mellor | db8aa8c | 2017-11-08 14:26:17 -0800 | [diff] [blame] | 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 | |
| 17 | package com.example.androidx.slice.demos; |
| 18 | |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 19 | import static com.example.androidx.slice.demos.SampleSliceProvider.getUri; |
| 20 | |
Mady Mellor | db8aa8c | 2017-11-08 14:26:17 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.net.wifi.WifiManager; |
| 25 | import android.os.Handler; |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 26 | import android.widget.Toast; |
Mady Mellor | db8aa8c | 2017-11-08 14:26:17 -0800 | [diff] [blame] | 27 | |
Jason Monk | 8b099f2 | 2017-11-15 15:15:48 -0500 | [diff] [blame] | 28 | import androidx.app.slice.core.SliceHints; |
Mady Mellor | db8aa8c | 2017-11-08 14:26:17 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Responds to actions performed on slices and notifies slices of updates in state changes. |
| 32 | */ |
| 33 | public class SliceBroadcastReceiver extends BroadcastReceiver { |
| 34 | |
| 35 | @Override |
| 36 | public void onReceive(Context context, Intent i) { |
| 37 | String action = i.getAction(); |
| 38 | switch (action) { |
| 39 | case SampleSliceProvider.ACTION_WIFI_CHANGED: |
| 40 | WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
| 41 | boolean newState = i.getBooleanExtra(SliceHints.EXTRA_TOGGLE_STATE, |
| 42 | wm.isWifiEnabled()); |
| 43 | wm.setWifiEnabled(newState); |
| 44 | // Wait a bit for wifi to update (TODO: is there a better way to do this?) |
| 45 | Handler h = new Handler(); |
| 46 | h.postDelayed(() -> { |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 47 | context.getContentResolver().notifyChange(getUri("wifi", context), null); |
Mady Mellor | db8aa8c | 2017-11-08 14:26:17 -0800 | [diff] [blame] | 48 | }, 1000); |
| 49 | break; |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 50 | case SampleSliceProvider.ACTION_TOAST: |
| 51 | String message = i.getExtras().getString(SampleSliceProvider.EXTRA_TOAST_MESSAGE, |
| 52 | "no message"); |
| 53 | Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); |
| 54 | break; |
Mady Mellor | db8aa8c | 2017-11-08 14:26:17 -0800 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |