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 static android.app.slice.Slice.EXTRA_RANGE_VALUE;
20import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
21
22import static com.example.androidx.slice.demos.SampleSliceProvider.getUri;
23
24import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.net.wifi.WifiManager;
28import android.os.Handler;
29import android.widget.Toast;
30
31/**
32 * Responds to actions performed on slices and notifies slices of updates in state changes.
33 */
34public class SliceBroadcastReceiver extends BroadcastReceiver {
35
36    @Override
37    public void onReceive(Context context, Intent i) {
38        String action = i.getAction();
39        switch (action) {
40            case SampleSliceProvider.ACTION_WIFI_CHANGED:
41                WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
42                boolean newState = i.getBooleanExtra(EXTRA_TOGGLE_STATE, 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(() -> {
47                    context.getContentResolver().notifyChange(getUri("wifi", context), null);
48                }, 1000);
49                break;
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;
55            case SampleSliceProvider.ACTION_TOAST_RANGE_VALUE:
56                int range = i.getExtras().getInt(EXTRA_RANGE_VALUE, 0);
57                Toast.makeText(context, "value: " + range, Toast.LENGTH_SHORT).show();
58                break;
59        }
60    }
61}
62