blob: 05f0820968ad0d7b7a955be475890a68d1e830d9 [file] [log] [blame]
Mady Mellordb8aa8c2017-11-08 14:26:17 -08001/*
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
Mady Mellorc1334182017-11-10 15:50:35 -080019import static com.example.androidx.slice.demos.SampleSliceProvider.getUri;
20
Mady Mellordb8aa8c2017-11-08 14:26:17 -080021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.net.wifi.WifiManager;
25import android.os.Handler;
Mady Mellorc1334182017-11-10 15:50:35 -080026import android.widget.Toast;
Mady Mellordb8aa8c2017-11-08 14:26:17 -080027
Jason Monk8b099f22017-11-15 15:15:48 -050028import androidx.app.slice.core.SliceHints;
Mady Mellordb8aa8c2017-11-08 14:26:17 -080029
30/**
31 * Responds to actions performed on slices and notifies slices of updates in state changes.
32 */
33public 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 Mellorc1334182017-11-10 15:50:35 -080047 context.getContentResolver().notifyChange(getUri("wifi", context), null);
Mady Mellordb8aa8c2017-11-08 14:26:17 -080048 }, 1000);
49 break;
Mady Mellorc1334182017-11-10 15:50:35 -080050 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 Mellordb8aa8c2017-11-08 14:26:17 -080055 }
56 }
57}