提交 3520449b 编写于 作者: A Adam Cummings 提交者: Eric Frohnhoefer

Add BroadcastReceiver for cancelling Compose Tweet

Change-Id: Iea2b2e0d0e01ea5d2f4a4d2a0d00916739202794
上级 365ddacf
......@@ -97,5 +97,13 @@
</intent-filter>
</receiver>
<receiver
android:name=".tweetcomposer.TweetCancelReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.twitter.sdk.android.tweetcomposer.TWEET_COMPOSE_CANCEL"/>
</intent-filter>
</receiver>
</application>
</manifest>
/*
* Copyright (C) 2015 Twitter, Inc.
*
* 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 com.example.app.tweetcomposer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class TweetCancelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TweetCancelReceiver", "User cancelled compose tweet");
}
}
......@@ -35,6 +35,7 @@ public class ComposerActivity extends Activity {
static final String EXTRA_HASHTAGS = "EXTRA_HASHTAGS";
private static final int PLACEHOLDER_ID = -1;
private static final String PLACEHOLDER_SCREEN_NAME = "";
private ComposerController composerController;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -52,7 +53,8 @@ public class ComposerActivity extends Activity {
setTheme(themeResId);
setContentView(R.layout.tw__activity_composer);
final ComposerView composerView = (ComposerView) findViewById(R.id.tw__composer_view);
new ComposerController(composerView, session, imageUri, text, hashtags, new FinisherImpl());
composerController = new ComposerController(composerView, session, imageUri, text, hashtags,
new FinisherImpl());
}
interface Finisher {
......@@ -67,6 +69,12 @@ public class ComposerActivity extends Activity {
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
composerController.onClose();
}
public static class Builder {
private final Context context;
private TwitterAuthToken token;
......
......@@ -30,6 +30,8 @@ import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.models.User;
import static com.twitter.sdk.android.tweetcomposer.TweetUploadService.TWEET_COMPOSE_CANCEL;
class ComposerController {
final ComposerView composerView;
final TwitterSession session;
......@@ -130,11 +132,16 @@ class ComposerController {
@Override
public void onCloseClick() {
dependencyProvider.getScribeClient().click(ScribeConstants.SCRIBE_CANCEL_ELEMENT);
finisher.finish();
onClose();
}
}
void onClose() {
dependencyProvider.getScribeClient().click(ScribeConstants.SCRIBE_CANCEL_ELEMENT);
sendCancelBroadcast();
finisher.finish();
}
int tweetTextLength(String text) {
if (TextUtils.isEmpty(text)) {
return 0;
......@@ -143,6 +150,12 @@ class ComposerController {
return dependencyProvider.getTweetValidator().getTweetLength(text);
}
void sendCancelBroadcast() {
final Intent intent = new Intent(TWEET_COMPOSE_CANCEL);
intent.setPackage(composerView.getContext().getPackageName());
composerView.getContext().sendBroadcast(intent);
}
static int remainingCharCount(int charCount) {
return Validator.MAX_TWEET_LENGTH - charCount;
}
......
......@@ -42,6 +42,8 @@ public class TweetUploadService extends IntentService {
= "com.twitter.sdk.android.tweetcomposer.UPLOAD_SUCCESS";
public static final String UPLOAD_FAILURE
= "com.twitter.sdk.android.tweetcomposer.UPLOAD_FAILURE";
public static final String TWEET_COMPOSE_CANCEL =
"com.twitter.sdk.android.tweetcomposer.TWEET_COMPOSE_CANCEL";
public static final String EXTRA_TWEET_ID = "EXTRA_TWEET_ID";
public static final String EXTRA_RETRY_INTENT = "EXTRA_RETRY_INTENT";
......
......@@ -35,6 +35,8 @@ import org.robolectric.RobolectricTestRunner;
import retrofit2.Call;
import static com.twitter.sdk.android.tweetcomposer.TweetUploadService.TWEET_COMPOSE_CANCEL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
......@@ -211,4 +213,16 @@ public class ComposerControllerTest {
verify(mockFinisher).finish();
verify(mockComposerScribeClient).click(eq(ScribeConstants.SCRIBE_CANCEL_ELEMENT));
}
@Test
public void testSendCancelBroadcast() {
controller = new ComposerController(mockComposerView, mockTwitterSession, Uri.EMPTY,
ANY_TEXT, ANY_HASHTAG, mockFinisher, mockDependencyProvider);
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
controller.sendCancelBroadcast();
verify(mockContext).sendBroadcast(intentCaptor.capture());
final Intent capturedIntent = intentCaptor.getValue();
assertEquals(TWEET_COMPOSE_CANCEL, capturedIntent.getAction());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册