Add Quick Settings API

It is a little bit limited right now, but it contains
the lifecycle of a tile getting added/removed, and
listening/not listening and clicks.

SysUI side will need some cleanup later on.

Change-Id: I4db803c8a271f8bf44f2ef710517969a84a95cf0
This commit is contained in:
Jason Monk
2015-11-06 15:47:26 -05:00
parent f91e74d9a9
commit bbadff8603
28 changed files with 1105 additions and 857 deletions

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.quicksettings;
import android.content.ComponentName;
import android.service.quicksettings.Tile;
/**
* @hide
*/
interface IQSService {
void updateQsTile(in Tile tile);
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.quicksettings;
import android.service.quicksettings.Tile;
import android.service.quicksettings.IQSService;
/**
* @hide
*/
oneway interface IQSTileService {
void setQSTile(in Tile tile);
void onTileAdded();
void onTileRemoved();
void onStartListening();
void onStopListening();
void onClick();
}

View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.quicksettings;
parcelable Tile;

View File

@@ -0,0 +1,186 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.quicksettings;
import android.content.ComponentName;
import android.graphics.drawable.Icon;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
/**
* A Tile holds the state of a tile that will be displayed
* in Quick Settings.
*
* A tile in Quick Settings exists as an icon with an accompanied label.
* It also may have content description for accessibility usability.
* The style and layout of the tile may change to match a given
* device.
*/
public final class Tile implements Parcelable {
private static final String TAG = "Tile";
private ComponentName mComponentName;
private IQSService mService;
private Icon mIcon;
private CharSequence mLabel;
private CharSequence mContentDescription;
/**
* @hide
*/
public Tile(Parcel source) {
readFromParcel(source);
}
/**
* @hide
*/
public Tile(ComponentName componentName, IQSService service) {
mComponentName = componentName;
mService = service;
}
/**
* @hide
*/
public ComponentName getComponentName() {
return mComponentName;
}
/**
* Gets the current icon for the tile.
*/
public Icon getIcon() {
return mIcon;
}
/**
* Sets the current icon for the tile.
*
* This icon is expected to be white on alpha, and may be
* tinted by the system to match it's theme.
*
* Does not take effect until {@link #updateTile()} is called.
*
* @param icon New icon to show.
*/
public void setIcon(Icon icon) {
this.mIcon = icon;
}
/**
* Gets the current label for the tile.
*/
public CharSequence getLabel() {
return mLabel;
}
/**
* Sets the current label for the tile.
*
* Does not take effect until {@link #updateTile()} is called.
*
* @param label New label to show.
*/
public void setLabel(CharSequence label) {
this.mLabel = label;
}
/**
* Gets the current content description for the tile.
*/
public CharSequence getContentDescription() {
return mContentDescription;
}
/**
* Sets the current content description for the tile.
*
* Does not take effect until {@link #updateTile()} is called.
*
* @param contentDescription New content description to use.
*/
public void setContentDescription(CharSequence contentDescription) {
this.mContentDescription = contentDescription;
}
@Override
public int describeContents() {
return 0;
}
/**
* Pushes the state of the Tile to Quick Settings to be displayed.
*/
public void updateTile() {
try {
mService.updateQsTile(this);
} catch (RemoteException e) {
Log.e(TAG, "Couldn't update tile");
}
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStrongInterface(mService);
if (mComponentName != null) {
dest.writeByte((byte) 1);
mComponentName.writeToParcel(dest, flags);
} else {
dest.writeByte((byte) 0);
}
if (mIcon != null) {
dest.writeByte((byte) 1);
mIcon.writeToParcel(dest, flags);
} else {
dest.writeByte((byte) 0);
}
TextUtils.writeToParcel(mLabel, dest, flags);
TextUtils.writeToParcel(mContentDescription, dest, flags);
}
private void readFromParcel(Parcel source) {
mService = IQSService.Stub.asInterface(source.readStrongBinder());
if (source.readByte() != 0) {
mComponentName = ComponentName.CREATOR.createFromParcel(source);
} else {
mComponentName = null;
}
if (source.readByte() != 0) {
mIcon = Icon.CREATOR.createFromParcel(source);
} else {
mIcon = null;
}
mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
}
public static final Creator<Tile> CREATOR = new Creator<Tile>() {
@Override
public Tile createFromParcel(Parcel source) {
return new Tile(source);
}
@Override
public Tile[] newArray(int size) {
return new Tile[size];
}
};
}

View File

@@ -0,0 +1,206 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.quicksettings;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
/**
* A QSTileService provides the user a tile that can be added to Quick Settings.
* Quick Settings is a space provided that allows the user to change settings and
* take quick actions without leaving the context of their current app.
*
* <p>The lifecycle of a QSTileService is different from some other services in
* that it may be unbound during parts of its lifecycle. Any of the following
* lifecycle events can happen indepently in a separate binding/creation of the
* service.</p>
*
* <ul>
* <li>When a tile is added by the user its QSTileService will be bound to and
* {@link #onTileAdded()} will be called.</li>
*
* <li>When a tile should be up to date and listing will be indicated by
* {@link #onStartListening()} and {@link #onStopListening()}.</li>
*
* <li>When the user removes a tile from Quick Settings {@link #onStopListening()}
* will be called.</li>
* </ul>
* <p>QSTileService will be detected by tiles that match the {@value #ACTION_QS_TILE}
* and require the permission "android.permission.BIND_QUICK_SETTINGS_TILE".
* The label and icon for the service will be used as the default label and
* icon for the tile. Here is an example QSTileService declaration.</p>
* <pre class="prettyprint">
* {@literal
* <service
* android:name=".MyQSTileService"
* android:label="@string/my_default_tile_label"
* android:icon="@drawable/my_default_icon_label"
* android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
* <intent-filter>
* <action android:name="android.intent.action.QS_TILE" />
* </intent-filter>
* </service>}
* </pre>
*
* @see Tile Tile for details about the UI of a Quick Settings Tile.
*/
public class TileService extends Service {
/**
* Action that identifies a Service as being a QSTileService.
*/
public static final String ACTION_QS_TILE = "android.service.quicksettings.action.QS_TILE";
private final H mHandler = new H(Looper.getMainLooper());
private boolean mListening = false;
private Tile mTile;
/**
* Called when the user adds this tile to Quick Settings.
* <p/>
* Note that this is not guaranteed to be called between {@link #onCreate()}
* and {@link #onStartListening()}, it will only be called when the tile is added
* and not on subsequent binds.
*/
public void onTileAdded() {
}
/**
* Called when the user removes this tile from Quick Settings.
*/
public void onTileRemoved() {
}
/**
* Called when this tile moves into a listening state.
* <p/>
* When this tile is in a listening state it is expected to keep the
* UI up to date. Any listeners or callbacks needed to keep this tile
* up to date should be registered here and unregistered in {@link #onStopListening()}.
*
* @see #getQsTile()
* @see Tile#updateTile()
*/
public void onStartListening() {
}
/**
* Called when this tile moves out of the listening state.
*/
public void onStopListening() {
}
/**
* Called when the user clicks on this tile.
*/
public void onClick() {
}
/**
* Gets the {@link Tile} for this service.
* <p/>
* This tile may be used to get or set the current state for this
* tile. This tile is only valid for updates between {@link #onStartListening()}
* and {@link #onStopListening()}.
*/
public final Tile getQsTile() {
return mTile;
}
@Override
public IBinder onBind(Intent intent) {
return new IQSTileService.Stub() {
@Override
public void setQSTile(Tile tile) throws RemoteException {
mHandler.obtainMessage(H.MSG_SET_TILE, tile).sendToTarget();
}
@Override
public void onTileRemoved() throws RemoteException {
mHandler.sendEmptyMessage(H.MSG_TILE_REMOVED);
}
@Override
public void onTileAdded() throws RemoteException {
mHandler.sendEmptyMessage(H.MSG_TILE_ADDED);
}
@Override
public void onStopListening() throws RemoteException {
mHandler.sendEmptyMessage(H.MSG_STOP_LISTENING);
}
@Override
public void onStartListening() throws RemoteException {
mHandler.sendEmptyMessage(H.MSG_START_LISTENING);
}
@Override
public void onClick() throws RemoteException {
mHandler.sendEmptyMessage(H.MSG_TILE_CLICKED);
}
};
}
private class H extends Handler {
private static final int MSG_SET_TILE = 1;
private static final int MSG_START_LISTENING = 2;
private static final int MSG_STOP_LISTENING = 3;
private static final int MSG_TILE_ADDED = 4;
private static final int MSG_TILE_REMOVED = 5;
private static final int MSG_TILE_CLICKED = 6;
public H(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_SET_TILE:
mTile = (Tile) msg.obj;
break;
case MSG_TILE_ADDED:
TileService.this.onTileRemoved();
break;
case MSG_TILE_REMOVED:
TileService.this.onTileAdded();
break;
case MSG_START_LISTENING:
if (mListening) {
mListening = false;
TileService.this.onStopListening();
}
break;
case MSG_STOP_LISTENING:
if (!mListening) {
mListening = true;
TileService.this.onStartListening();
}
break;
case MSG_TILE_CLICKED:
TileService.this.onClick();
break;
}
}
}
}