Skip to content

Commit

Permalink
stops refresh service completely when no account enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Dec 30, 2015
1 parent cdd3cce commit 5048d92
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.mariotaku.restfu.annotation.method.POST;
import org.mariotaku.restfu.annotation.param.Form;
import org.mariotaku.restfu.annotation.param.Query;
import org.mariotaku.twidere.api.twitter.model.CardDataMap;
import org.mariotaku.twidere.api.twitter.model.CardEntity;
import org.mariotaku.twidere.api.twitter.model.CreateCardData;
import org.mariotaku.twidere.api.twitter.model.CreateCardResult;
Expand All @@ -42,6 +43,9 @@ CardEntity getCard(@NonNull @Query("twitter:string:card_uri") String cardUri,
@NonNull @Query("twitter:string:response_card_name") String responseCardName)
throws TwitterException;

@GET("/v2/capi/passthrough/1")
CardEntity sendPassThrough(@Form CardDataMap data) throws TwitterException;

@POST("/v2/cards/create.json")
CreateCardResult createCard(@Form("card_data") CreateCardData cardData) throws TwitterException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
package org.mariotaku.twidere.api.twitter.api;

import org.mariotaku.restfu.annotation.method.GET;
import org.mariotaku.restfu.annotation.method.POST;
import org.mariotaku.restfu.annotation.param.MethodExtra;
import org.mariotaku.restfu.annotation.param.Query;
import org.mariotaku.twidere.api.twitter.TwitterException;
import org.mariotaku.twidere.api.twitter.model.Activity;
import org.mariotaku.twidere.api.twitter.model.CursorResponse;
import org.mariotaku.twidere.api.twitter.model.CursorTimestampResponse;
import org.mariotaku.twidere.api.twitter.model.Paging;
import org.mariotaku.twidere.api.twitter.model.ResponseList;
import org.mariotaku.twidere.api.twitter.model.TimestampResponse;

@SuppressWarnings("RedundantThrows")
@MethodExtra(name = "extra_params", values = {"include_my_retweet", "include_rts", "include_entities",
Expand All @@ -38,11 +38,12 @@ public interface PrivateActivityResources extends PrivateResources {
@GET("/activity/about_me.json")
ResponseList<Activity> getActivitiesAboutMe(@Query Paging paging) throws TwitterException;

@GET("/activity/about_me/unread.json?cursor=true")
CursorResponse getActivitiesAboutMeUnreadCursor() throws TwitterException;
@GET("/activity/about_me/unread.json")
CursorTimestampResponse getActivitiesAboutMeUnread(@Query("cursor") boolean cursor) throws TwitterException;

@POST("/activity/about_me/unread.json")
CursorTimestampResponse setActivitiesAboutMeUnread(@Query("cursor") long cursor) throws TwitterException;

@GET("/activity/about_me/unread.json?cursor=false")
TimestampResponse getActivitiesAboutMeUnreadTimestamp() throws TwitterException;

@GET("/activity/by_friends.json")
ResponseList<Activity> getActivitiesByFriends(@Query Paging paging) throws TwitterException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.mariotaku.twidere.api.twitter.model;

import com.bluelinelabs.logansquare.LoganSquare;

import org.mariotaku.restfu.http.ValueMap;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
* Created by mariotaku on 15/12/30.
*/
public class CardDataMap implements ValueMap {
protected final Map<String, String> map = new LinkedHashMap<>();

public void putString(String key, String value) {
map.put("twitter:string:" + key, value);
}

public void putLong(String key, long value) {
map.put("twitter:long:" + key, String.valueOf(value));
}

@Override
public String toString() {
try {
return LoganSquare.serialize(map);
} catch (IOException e) {
throw new AssertionError(e);
}
}

@Override
public boolean has(String key) {
return map.containsKey(key);
}

@Override
public Object get(String key) {
return map.get(key);
}

@Override
public String[] keys() {
final Set<String> keySet = map.keySet();
return keySet.toArray(new String[keySet.size()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
* </pre>
* Created by mariotaku on 15/12/30.
*/
public class CreateCardData {

private final Map<String, String> map = new LinkedHashMap<>();
public class CreateCardData extends CardDataMap {

public CreateCardData(String name) {
this(name, "1");
Expand All @@ -49,21 +47,4 @@ public CreateCardData(String name, String endpoint) {
map.put("twitter:api:api:endpoint", endpoint);
}

public void putString(String key, String value) {
map.put("twitter:string:" + key, value);
}

public void putLong(String key, long value) {
map.put("twitter:long:" + key, String.valueOf(value));
}

@Override
public String toString() {
try {
return LoganSquare.serialize(map);
} catch (IOException e) {
throw new AssertionError(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@
* Created by mariotaku on 15/12/30.
*/
@JsonObject
public class CursorResponse {
public class CursorTimestampResponse {
@JsonField(name = "cursor")
long cursor;
@JsonField(name = "timestamp")
long timestamp;

public long getTimestamp() {
return timestamp;
}

public long getCursor() {
return cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import org.mariotaku.twidere.util.BugReporter;
import org.mariotaku.twidere.util.DebugModeUtils;
import org.mariotaku.twidere.util.ExternalThemeManager;
import org.mariotaku.twidere.util.TwidereMathUtils;
import org.mariotaku.twidere.util.StrictModeUtils;
import org.mariotaku.twidere.util.TwidereBugReporter;
import org.mariotaku.twidere.util.TwidereMathUtils;
import org.mariotaku.twidere.util.Utils;
import org.mariotaku.twidere.util.content.TwidereSQLiteOpenHelper;
import org.mariotaku.twidere.util.dagger.ApplicationModule;
Expand Down Expand Up @@ -233,6 +233,9 @@ public void onSharedPreferenceChanged(final SharedPreferences preferences, final
case KEY_CONNECTION_TIMEOUT:
case KEY_PROXY_HOST:
case KEY_PROXY_PORT:
case KEY_PROXY_TYPE:
case KEY_PROXY_USERNAME:
case KEY_PROXY_PASSWORD:
reloadConnectivitySettings();
break;
case KEY_DNS_SERVER:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,34 @@

package org.mariotaku.twidere.fragment;

import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import org.mariotaku.twidere.R;
import org.mariotaku.twidere.util.Utils;

public class AccountRefreshSettingsFragment extends BaseAccountPreferenceFragment {

@Override
protected int getPreferencesResource() {
return R.xml.preferences_account_refresh;
}

@Override
protected boolean getSwitchPreferenceDefault() {
return DEFAULT_AUTO_REFRESH;
}

@Override
@Nullable
protected String getSwitchPreferenceKey() {
return null;
}
@Override
protected int getPreferencesResource() {
return R.xml.preferences_account_refresh;
}

@Override
protected boolean getSwitchPreferenceDefault() {
return DEFAULT_AUTO_REFRESH;
}

@Override
@Nullable
protected String getSwitchPreferenceKey() {
return null;
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (KEY_AUTO_REFRESH.equals(key)) {
Utils.startRefreshServiceIfNeeded(getActivity());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
/**
* Created by mariotaku on 15/12/20.
*/
public class CardPollFragment extends BaseSupportFragment {
public class CardPollFragment extends BaseSupportFragment implements View.OnClickListener {

public static final Pattern PATTERN_POLL_TEXT_ONLY = Pattern.compile("poll([\\d]+)choice_text_only");
private TableLayout mPollContainer;
Expand All @@ -74,6 +74,7 @@ public static boolean isPoll(@NonNull String name) {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mVoteButton.setOnClickListener(this);
initChoiceView(savedInstanceState);
}

Expand Down Expand Up @@ -177,6 +178,11 @@ private ParcelableStatus getStatus() {
return getArguments().getParcelable(EXTRA_STATUS);
}

@Override
public void onClick(View v) {

}

private static class PercentDrawable extends Drawable {

private final Paint mPaint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import org.mariotaku.twidere.Constants;
Expand Down Expand Up @@ -134,8 +133,9 @@ public static AccountPreferences[] getAccountPreferences(final Context context,
return preferences;
}

@NonNull
public static long[] getAutoRefreshEnabledAccountIds(final Context context, final long[] accountIds) {
if (context == null || accountIds == null) return null;
if (context == null || accountIds == null) return new long[0];
final long[] temp = new long[accountIds.length];
int i = 0;
for (final long accountId : accountIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ public void onCreate() {
registerReceiver(mPowerStateReceiver, batteryFilter);
registerReceiver(mScreenStateReceiver, screenFilter);
PowerStateReceiver.setServiceReceiverStarted(true);
startAutoRefresh();
if (hasAutoRefreshAccounts(this)) {
startAutoRefresh();
} else {
stopSelf();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.mariotaku.twidere.BuildConfig;
import org.mariotaku.twidere.TwidereConstants;
import org.mariotaku.twidere.api.twitter.Twitter;
import org.mariotaku.twidere.api.twitter.TwitterCaps;
import org.mariotaku.twidere.api.twitter.TwitterException;
import org.mariotaku.twidere.api.twitter.TwitterOAuth;
import org.mariotaku.twidere.api.twitter.TwitterUpload;
Expand Down Expand Up @@ -192,7 +193,8 @@ public Request authenticateProxy(Proxy proxy, Response response) throws IOExcept
}
});
} else {
client.setProxySelector(ProxySelector.getDefault());
client.setProxySelector(null);
client.setAuthenticator(null);
}
}

Expand Down Expand Up @@ -298,6 +300,9 @@ public static Endpoint getEndpoint(ParcelableCredentials credentials, Class<?> c
} else if (TwitterUserStream.class.isAssignableFrom(cls)) {
domain = "userstream";
versionSuffix = noVersionSuffix ? null : "/1.1/";
} else if (TwitterCaps.class.isAssignableFrom(cls)) {
domain = "caps";
versionSuffix = null;
} else {
throw new TwitterConverter.UnsupportedTypeException(cls);
}
Expand Down
3 changes: 1 addition & 2 deletions twidere/src/main/java/org/mariotaku/twidere/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1830,8 +1830,7 @@ public static boolean hasAccountSignedWithOfficialKeys(final Context context) {

public static boolean hasAutoRefreshAccounts(final Context context) {
final long[] accountIds = getAccountIds(context);
final long[] refreshIds = AccountPreferences.getAutoRefreshEnabledAccountIds(context, accountIds);
return refreshIds != null && refreshIds.length > 0;
return !ArrayUtils.isEmpty(AccountPreferences.getAutoRefreshEnabledAccountIds(context, accountIds));
}

public static boolean hasStaggeredTimeline() {
Expand Down

0 comments on commit 5048d92

Please sign in to comment.