SampleSliceProvider.java revision db8aa8c68d0e51eddc1f58140e292fe5164bb52b
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.app.PendingIntent;
20import android.app.slice.Slice;
21import android.app.slice.SliceProvider;
22import android.content.Context;
23import android.content.Intent;
24import android.graphics.drawable.Icon;
25import android.net.Uri;
26import android.net.wifi.WifiManager;
27import android.provider.Settings;
28import android.support.annotation.RequiresApi;
29import android.text.format.DateUtils;
30
31import androidx.app.slice.builders.MessagingSliceBuilder;
32import androidx.app.slice.builders.SliceHints;
33
34/**
35 * Examples of using slice template builders.
36 */
37@RequiresApi(api = 28)
38public class SampleSliceProvider extends SliceProvider {
39    public static final Uri MESSAGE =
40            Uri.parse("content://com.example.androidx.slice.demos/message");
41    public static final Uri WIFI =
42            Uri.parse("content://com.example.androidx.slice.demos/wifi");
43
44    public static final String ACTION_WIFI_CHANGED =
45            "com.android.settings.slice.action.WIFI_CHANGED";
46
47    @Override
48    public boolean onCreate() {
49        return true;
50    }
51
52    @Override
53    public Slice onBindSlice(Uri sliceUri) {
54        String path = sliceUri.getPath();
55        switch (path) {
56            case "/message":
57                return createMessagingSlice(sliceUri);
58            case "/wifi":
59                return createSettingsSlice(sliceUri);
60        }
61        throw new IllegalArgumentException("Unknown uri " + sliceUri);
62    }
63
64    private Slice createMessagingSlice(Uri sliceUri) {
65        // TODO: Remote input.
66        return new MessagingSliceBuilder(sliceUri)
67                .startMessage()
68                        .addText("yo home \uD83C\uDF55, I emailed you the info")
69                        .addTimestamp(System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS)
70                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady))
71                        .endMessage()
72                .startMessage()
73                        .addText("just bought my tickets")
74                        .addTimestamp(System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS)
75                        .endMessage()
76                .startMessage()
77                        .addText("yay! can't wait for getContext() weekend!\n"
78                                + "\uD83D\uDE00")
79                        .addTimestamp(System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS)
80                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady))
81                        .endMessage()
82                .build();
83
84    }
85
86    private Slice createSettingsSlice(Uri sliceUri) {
87        // TODO: Create a proper template builder for toggles
88        // Get wifi state
89        String[] toggleHints;
90        WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
91        int wifiState = wifiManager.getWifiState();
92        boolean wifiEnabled = false;
93        String state;
94        switch (wifiState) {
95            case WifiManager.WIFI_STATE_DISABLED:
96            case WifiManager.WIFI_STATE_DISABLING:
97                state = "disconnected";
98                break;
99            case WifiManager.WIFI_STATE_ENABLED:
100            case WifiManager.WIFI_STATE_ENABLING:
101                state = wifiManager.getConnectionInfo().getSSID();
102                wifiEnabled = true;
103                break;
104            case WifiManager.WIFI_STATE_UNKNOWN:
105            default:
106                state = ""; // just don't show anything?
107                break;
108        }
109        if (wifiEnabled) {
110            toggleHints = new String[] {SliceHints.HINT_TOGGLE, Slice.HINT_SELECTED};
111        } else {
112            toggleHints = new String[] {SliceHints.HINT_TOGGLE};
113        }
114        // Construct the slice
115        Slice.Builder b = new Slice.Builder(sliceUri);
116        b.addSubSlice(new Slice.Builder(b)
117                .addAction(getIntent(Settings.ACTION_WIFI_SETTINGS),
118                        new Slice.Builder(b)
119                                .addText("Wi-fi")
120                                .addText(state)
121                                .addIcon(Icon.createWithResource(getContext(),
122                                        R.drawable.ic_settings_wifi), SliceHints.HINT_HIDDEN)
123                                .addHints(Slice.HINT_TITLE)
124                                .build())
125                .addAction(getBroadcastIntent(ACTION_WIFI_CHANGED),
126                        new Slice.Builder(b)
127                                .addHints(toggleHints)
128                                .build())
129                .build());
130        return b.build();
131    }
132
133    private PendingIntent getIntent(String action) {
134        Intent intent = new Intent(action);
135        PendingIntent pi = PendingIntent.getActivity(getContext(), 0, intent, 0);
136        return pi;
137    }
138
139    private PendingIntent getBroadcastIntent(String action) {
140        Intent intent = new Intent(action);
141        intent.setClass(getContext(), SliceBroadcastReceiver.class);
142        return PendingIntent.getBroadcast(getContext(), 0, intent,
143                PendingIntent.FLAG_CANCEL_CURRENT);
144    }
145}
146