From 8d4920022f5b09a07e257fdf61a102a140c2f267 Mon Sep 17 00:00:00 2001 From: Roman Birg Date: Thu, 14 Jan 2016 09:44:13 -0800 Subject: [PATCH] CustomTiles: add a custom long press PendingIntent Ref: CYNGNOS-1602 Change-Id: Id0cca88fabb091dcf0fbad2ae24416fa1c0af83e Signed-off-by: Roman Birg --- src/java/cyanogenmod/app/CustomTile.java | 40 +++++++++++++++++++ .../tests/customtiles/CMStatusBarTest.java | 15 +++++++ .../unit/CustomTileBuilderTest.java | 11 +++++ .../customtiles/unit/CustomTileTest.java | 23 +++++++++++ 4 files changed, 89 insertions(+) diff --git a/src/java/cyanogenmod/app/CustomTile.java b/src/java/cyanogenmod/app/CustomTile.java index 4ab71c1..26ff6b4 100644 --- a/src/java/cyanogenmod/app/CustomTile.java +++ b/src/java/cyanogenmod/app/CustomTile.java @@ -53,6 +53,15 @@ public class CustomTile implements Parcelable { **/ public PendingIntent onClick; + /** + * An optional intent to execute when the custom tile entry is long clicked. If + * this is an activity, it must include the + * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires + * that you take care of task management. Activities will also automatically trigger + * the host panel to automatically collapse after executing the pending intent. + **/ + public PendingIntent onLongClick; + /** * An optional settings intent to execute when the custom tile's detail is shown * If this is an activity, it must include the @@ -162,6 +171,12 @@ public class CustomTile implements Parcelable { this.sensitiveData = (parcel.readInt() == 1); } + if (parcelableVersion >= Build.CM_VERSION_CODES.DRAGON_FRUIT) { + if (parcel.readInt() != 0) { + this.onLongClick = PendingIntent.CREATOR.createFromParcel(parcel); + } + } + parcel.setDataPosition(startPosition + parcelableSize); } @@ -196,6 +211,9 @@ public class CustomTile implements Parcelable { if (onClick != null) { b.append("onClick=" + onClick.toString() + NEW_LINE); } + if (onLongClick != null) { + b.append("onLongClick=" + onLongClick.toString() + NEW_LINE); + } if (onSettingsClick != null) { b.append("onSettingsClick=" + onSettingsClick.toString() + NEW_LINE); } @@ -229,6 +247,7 @@ public class CustomTile implements Parcelable { public void cloneInto(CustomTile that) { that.resourcesPackageName = this.resourcesPackageName; that.onClick = this.onClick; + that.onLongClick = this.onLongClick; that.onSettingsClick = this.onSettingsClick; that.onClickUri = this.onClickUri; that.label = this.label; @@ -314,6 +333,14 @@ public class CustomTile implements Parcelable { } out.writeInt(sensitiveData ? 1 : 0); + // ==== DRAGONFRUIT ==== + if (onLongClick != null) { + out.writeInt(1); + onLongClick.writeToParcel(out, 0); + } else { + out.writeInt(0); + } + // Go back and write size int parcelableSize = out.dataPosition() - startPosition; out.setDataPosition(sizePosition); @@ -907,6 +934,7 @@ public class CustomTile implements Parcelable { */ public static class Builder { private PendingIntent mOnClick; + private PendingIntent mOnLongClick; private Intent mOnSettingsClick; private Uri mOnClickUri; private String mLabel; @@ -976,6 +1004,17 @@ public class CustomTile implements Parcelable { return this; } + /** + * Set a {@link android.app.PendingIntent} to be fired on custom tile long press. + * Note: if this is an activity, the host panel will automatically collapse. + * @param intent + * @return {@link cyanogenmod.app.CustomTile.Builder} + */ + public Builder setOnLongClickIntent(PendingIntent intent) { + mOnLongClick = intent; + return this; + } + /** * Set a settings {@link android.content.Intent} to be fired on custom * tile detail pane click @@ -1080,6 +1119,7 @@ public class CustomTile implements Parcelable { CustomTile tile = new CustomTile(); tile.resourcesPackageName = mContext.getPackageName(); tile.onClick = mOnClick; + tile.onLongClick = mOnLongClick; tile.onSettingsClick = mOnSettingsClick; tile.onClickUri = mOnClickUri; tile.label = mLabel; diff --git a/tests/src/org/cyanogenmod/tests/customtiles/CMStatusBarTest.java b/tests/src/org/cyanogenmod/tests/customtiles/CMStatusBarTest.java index 20c9809..38ddca8 100644 --- a/tests/src/org/cyanogenmod/tests/customtiles/CMStatusBarTest.java +++ b/tests/src/org/cyanogenmod/tests/customtiles/CMStatusBarTest.java @@ -148,6 +148,21 @@ public class CMStatusBarTest extends TestActivity { } }, + new Test("test publish tile with long press") { + public void run() { + CustomTile customTile = new CustomTile.Builder(CMStatusBarTest.this) + .setLabel("Test Long press From SDK") + .setIcon(R.drawable.ic_launcher) + .setOnLongClickIntent(PendingIntent.getActivity(CMStatusBarTest.this, 0, + new Intent(CMStatusBarTest.this,DummySettings.class) + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0)) + .setContentDescription("Content description") + .build(); + CMStatusBarManager.getInstance(CMStatusBarTest.this) + .publishTile(CUSTOM_TILE_SETTINGS_ID, customTile); + } + }, + new Test("test publish tile with delete intent") { public void run() { Intent intent = new Intent(CMStatusBarTest.this, DummySettings.class); diff --git a/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileBuilderTest.java b/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileBuilderTest.java index 128ef77..5d20136 100644 --- a/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileBuilderTest.java +++ b/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileBuilderTest.java @@ -57,6 +57,17 @@ public class CustomTileBuilderTest extends AndroidTestCase { assertEquals(pendingIntent, customTile.onClick); } + @SmallTest + public void testCustomTileBuilderOnLongClickIntent() { + Intent intent = new Intent(Intent.ACTION_DIAL); + PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); + CustomTile customTile = new CustomTile.Builder(mContext) + .setOnLongClickIntent(pendingIntent) + .build(); + assertNotNull(customTile.onLongClick); + assertEquals(pendingIntent, customTile.onLongClick); + } + @SmallTest public void testCustomTileBuilderOnSettingsClickIntent() { Intent intent = new Intent(mContext, DummySettings.class); diff --git a/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileTest.java b/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileTest.java index b724550..ca04f56 100644 --- a/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileTest.java +++ b/tests/src/org/cyanogenmod/tests/customtiles/unit/CustomTileTest.java @@ -64,6 +64,29 @@ public class CustomTileTest extends AndroidTestCase { fromParcel.onClick.getIntent().toString()); } + @SmallTest + public void testCustomTileOnLongiClickIntentUnravelFromParcel() { + Intent intent = new Intent(Intent.ACTION_DIAL); + PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); + CustomTile expectedCustomTile = new CustomTile.Builder(mContext) + .setOnLongClickIntent(pendingIntent) + .build(); + + // Write to parcel + Parcel parcel = Parcel.obtain(); + expectedCustomTile.writeToParcel(parcel, 0); + + // Rewind + parcel.setDataPosition(0); + + // Verify data when unraveling + CustomTile fromParcel = CustomTile.CREATOR.createFromParcel(parcel); + + assertNotNull(fromParcel.onLongClick); + assertEquals(expectedCustomTile.onLongClick.getIntent().toString(), + fromParcel.onLongClick.getIntent().toString()); + } + @SmallTest public void testCustomTileOnSettingsClickIntentUnravelFromParcel() { Intent intent = new Intent(mContext, DummySettings.class);