Skip to content

Commit

Permalink
#384 Got rid of multi-step commands. Removed AUTOMATIC_UPDATE command…
Browse files Browse the repository at this point in the history
… as now only concrete commands e.g. "FETCH_TIMELINE" are put into a queue.
  • Loading branch information
yvolk committed Jun 18, 2016
1 parent 26375ba commit a02ab40
Show file tree
Hide file tree
Showing 27 changed files with 275 additions and 448 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '24.0.0 rc3'
buildToolsVersion '24.0.0'

defaultConfig {
applicationId "org.andstatus.app"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.andstatus.app.net.social.MbUser;
import org.andstatus.app.service.AttachmentDownloaderTest;
import org.andstatus.app.service.CommandData;
import org.andstatus.app.service.CommandEnum;
import org.andstatus.app.service.CommandExecutionContext;
import org.andstatus.app.timeline.TimelineType;
import org.andstatus.app.util.SelectionAndArgs;
Expand All @@ -56,8 +57,8 @@ public void testFriends() throws ConnectionException {
String messageOid = "https://identi.ca/api/comment/dasdjfdaskdjlkewjz1EhSrTRB";
MessageInserter.deleteOldMessage(TestSuite.getConversationOriginId(), messageOid);

CommandExecutionContext counters = new CommandExecutionContext(CommandData.getEmpty(),
TestSuite.getConversationMyAccount()).setTimelineType(TimelineType.HOME);
CommandExecutionContext counters = new CommandExecutionContext(CommandData.newCommand(CommandEnum.EMPTY,
TestSuite.getConversationMyAccount())).setTimelineType(TimelineType.HOME);
DataInserter di = new DataInserter(counters);
String username = "somebody@identi.ca";
String userOid = "acct:" + username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.andstatus.app.net.social.MbUser;
import org.andstatus.app.origin.Origin;
import org.andstatus.app.service.CommandData;
import org.andstatus.app.service.CommandEnum;
import org.andstatus.app.service.CommandExecutionContext;
import org.andstatus.app.timeline.TimelineType;

Expand Down Expand Up @@ -140,7 +141,8 @@ private MbMessage buildMessage(MbUser author, String body, MbMessage inReplyToMe
}

private long addMessage(MbMessage message) {
DataInserter di = new DataInserter(new CommandExecutionContext(CommandData.getEmpty(), ma).setTimelineType(TimelineType.HOME));
DataInserter di = new DataInserter(new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(TimelineType.HOME));
long messageId = di.insertOrUpdateMsg(message);
assertTrue( "Message added " + message.oid, messageId != 0);
return messageId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.andstatus.app.origin.Origin;
import org.andstatus.app.origin.OriginType;
import org.andstatus.app.service.CommandData;
import org.andstatus.app.service.CommandEnum;
import org.andstatus.app.service.CommandExecutionContext;
import org.andstatus.app.timeline.TimelineType;
import org.andstatus.app.util.InstanceId;
Expand Down Expand Up @@ -142,7 +143,8 @@ public long addMessage(MbMessage messageIn) {
if (messageIn.isPublic() ) {
tt = TimelineType.PUBLIC;
}
DataInserter di = new DataInserter(new CommandExecutionContext(CommandData.getEmpty(), ma).setTimelineType(tt));
DataInserter di = new DataInserter(new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(tt));
long messageId = di.insertOrUpdateMsg(messageIn);
assertTrue( "Message added " + messageIn.oid, messageId != 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public void testPriority() {
queue.add(CommandData.newSearch(
TestSuite.getMyAccount(TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME), "q1"));
queue.add(CommandData.newUpdateStatus(null, 2));
queue.add(CommandData.newCommand(CommandEnum.AUTOMATIC_UPDATE));
queue.add(CommandData.newCommand(CommandEnum.FETCH_TIMELINE));
queue.add(CommandData.newUpdateStatus(null, 3));
queue.add(CommandData.newCommand(CommandEnum.GET_STATUS));

assertEquals(CommandEnum.UPDATE_STATUS, queue.poll().getCommand());
assertEquals(CommandEnum.UPDATE_STATUS, queue.poll().getCommand());
assertEquals(CommandEnum.GET_STATUS, queue.poll().getCommand());
assertEquals(CommandEnum.SEARCH_MESSAGE, queue.poll().getCommand());
assertEquals(CommandEnum.AUTOMATIC_UPDATE, queue.poll().getCommand());
assertEquals(CommandEnum.FETCH_TIMELINE, queue.poll().getCommand());
}

public void testSummary() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ protected void setUp() throws Exception {
}

public void testHomeAccumulation() {
CommandExecutionContext execContext = new CommandExecutionContext(CommandData.getEmpty(), ma).setTimelineType(TimelineType.HOME);
CommandExecutionContext execContext = new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(TimelineType.HOME);
assertEquals(execContext.getTimelineType(), TimelineType.HOME);

final int MESSAGES = 4;
Expand All @@ -36,7 +37,8 @@ public void testHomeAccumulation() {
}

public void testDirectAccumulation() {
CommandExecutionContext execContext = new CommandExecutionContext(CommandData.getEmpty(), ma).setTimelineType(TimelineType.DIRECT);
CommandExecutionContext execContext = new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(TimelineType.DIRECT);
final int MESSAGES = 4;
for (int ind=0; ind < MESSAGES; ind++) {
execContext.getResult().incrementMessagesCount(execContext.getTimelineType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ protected void setUp() throws Exception {
public void testFetchTimeline() {
CommandData commandData = CommandData.newCommand(CommandEnum.FETCH_TIMELINE);
CommandExecutorStrategy strategy = CommandExecutorStrategy.getStrategy(commandData, null);
assertEquals(CommandExecutorAllAccounts.class, strategy.getClass());
assertEquals(CommandExecutorStrategy.class, strategy.getClass());

commandData = CommandData.newCommand(CommandEnum.FETCH_TIMELINE, ma);
strategy = CommandExecutorStrategy.getStrategy(commandData, null);
assertEquals(CommandExecutorLoadTimeline.class, strategy.getClass());
}

public void testSearch() {
CommandData commandData = CommandData.newSearch(null, TestSuite.GLOBAL_PUBLIC_MESSAGE_TEXT);
CommandExecutorStrategy strategy = CommandExecutorStrategy.getStrategy(commandData, null);
assertEquals(CommandExecutorAllOrigins.class, strategy.getClass());
assertEquals(CommandExecutorStrategy.class, strategy.getClass());

commandData = CommandData.newSearch(ma, TestSuite.GLOBAL_PUBLIC_MESSAGE_TEXT);
strategy = CommandExecutorStrategy.getStrategy(commandData, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.andstatus.app.service;

import android.content.SyncResult;
import android.test.InstrumentationTestCase;

import org.andstatus.app.account.MyAccount;
Expand Down Expand Up @@ -80,22 +81,32 @@ public void testRepeatingFailingCommand() throws MalformedURLException {
MyLog.v(this, method + " ended");
}

public void testAutomaticUpdates() {
MyLog.v(this, "testAutomaticUpdates started");
public void testAccountSync() {
final String method = "testAccountSync";
MyLog.v(this, method + " started");

mService.listenedCommand = CommandData.newTimelineCommand(
CommandEnum.AUTOMATIC_UPDATE, null, TimelineType.EVERYTHING);
long startCount = mService.executionStartCount;
long endCount = mService.executionEndCount;

mService.sendListenedToCommand();

assertTrue("First command started executing", mService.waitForCommandExecutionStarted(startCount));
assertTrue("First command ended executing", mService.waitForCommandExecutionEnded(endCount));
assertTrue(mService.httpConnectionMock.toString(),
MyAccount myAccount = MyContextHolder.get().persistentAccounts().findFirstSucceededMyAccountByOriginId(0);
assertTrue("No successful account", myAccount != null);

SyncResult syncResult = new SyncResult();
MyServiceCommandsRunner runner = new MyServiceCommandsRunner(getInstrumentation().getTargetContext());
runner.syncAccount(myAccount.getAccountName(), syncResult);
assertFalse(runner.toString(), runner.isSyncCompleted());
assertTrue(mService.httpConnectionMock.getRequestsCounter() + " requests were sent" +
" while service was unavailable. " +
mService.httpConnectionMock.toString(),
mService.httpConnectionMock.getRequestsCounter() == 0);

syncResult = new SyncResult();
runner = new MyServiceCommandsRunner(getInstrumentation().getTargetContext());
runner.setIgnoreServiceAvailability(true);
runner.syncAccount(myAccount.getAccountName(), syncResult);
assertTrue(runner.toString(), runner.isSyncCompleted());
assertTrue(runner.toString() + "; " + mService.httpConnectionMock.toString(),
mService.httpConnectionMock.getRequestsCounter() > 1);

assertTrue("Service stopped", mService.waitForServiceStopped(true));
MyLog.v(this, "testAutomaticUpdates ended");
MyLog.v(this, method + " ended");
}

public void testHomeTimeline() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public void setUp(String accountName) {
}
TestSuite.getMyContextForTest().setConnectionState(ConnectionState.WIFI);
MyAccountTest.fixPersistentAccounts();
// In order for the mocked connection to have effect:
// In order for the mocked connection to have an effect:
MyContextHolder.get().persistentAccounts().initialize();
MyContextHolder.get().persistentTimelines().initialize();
MyAccount ma = MyContextHolder.get().persistentAccounts().fromAccountName(accountName);
TestCase.assertTrue("HttpConnection mocked", ma.getConnection().getHttp() instanceof HttpConnectionMock);
if (!isSingleMockedInstance) {
Expand Down Expand Up @@ -166,6 +167,7 @@ public void tearDown() {
TestSuite.setHttpConnectionMockInstance(null);
TestSuite.getMyContextForTest().setConnectionState(ConnectionState.UNKNOWN);
MyContextHolder.get().persistentAccounts().initialize();
MyContextHolder.get().persistentTimelines().initialize();
MyLog.v(this, "tearDown ended");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ protected void onPostExecute(JSONObject jso) {
if (succeeded) {
String accountName = state.getAccount().getAccountName();
MyContextHolder.get().persistentAccounts().initialize();
MyContextHolder.get().persistentTimelines().initialize();
state.builder = MyAccount.Builder.newOrExistingFromAccountName(
MyContextHolder.get(), accountName, TriState.TRUE);
updateScreen();
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andstatus/app/account/MyAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ SaveResult saveSilently() {
myAccount.accountData.setDataBoolean(MyAccount.KEY_IS_SYNCABLE, myAccount.mIsSyncable);
myAccount.accountData.setDataBoolean(MyAccount.KEY_SYNC_AUTOMATICALLY, myAccount.mSyncAutomatically);
if (myAccount.syncFrequencySeconds == 0) {
myAccount.syncFrequencySeconds = MyPreferences.getSyncFrequencySeconds();
setSyncFrequency(MyPreferences.getSyncFrequencySeconds());
}
myAccount.accountData.setDataLong(MyPreferences.KEY_SYNC_FREQUENCY_SECONDS, myAccount.syncFrequencySeconds);
myAccount.accountData.setDataLong(MyPreferences.KEY_SYNC_FREQUENCY_SECONDS, myAccount.syncFrequencySeconds);
myAccount.accountData.setDataInt(KEY_VERSION, myAccount.version);
if (androidAccount != null) {
myAccount.accountData.saveDataToAccount(myContext, androidAccount, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ public long onRestore(MyBackupDataInput data, MyBackupDescriptor newDescriptor)
return restoredCount;
}

@Override
public String toString() {
return "PersistentAccounts{" +
"mAccounts=" + mAccounts +
'}';
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/andstatus/app/data/DataInserter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.andstatus.app.net.social.TimelinePosition;
import org.andstatus.app.service.AttachmentDownloader;
import org.andstatus.app.service.CommandData;
import org.andstatus.app.service.CommandEnum;
import org.andstatus.app.service.CommandExecutionContext;
import org.andstatus.app.timeline.TimelineType;
import org.andstatus.app.util.MyLog;
Expand All @@ -61,7 +62,7 @@ public class DataInserter {
SharedPreferencesUtil.getString(MyPreferences.KEY_FILTER_HIDE_MESSAGES_BASED_ON_KEYWORDS, ""));

public DataInserter(MyAccount ma) {
this(new CommandExecutionContext(CommandData.getEmpty(), ma));
this(new CommandExecutionContext(CommandData.newCommand(CommandEnum.EMPTY, ma)));
}

public DataInserter(CommandExecutionContext execContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ protected void onPause() {
private void clearNotifications() {
MyContextHolder.get().clearNotification(getTimelineType());
MyServiceManager.sendForegroundCommand(
CommandData.newCommand(CommandEnum.NOTIFY_CLEAR, paramsNew.getMyAccount()));
CommandData.newCommand(CommandEnum.CLEAR_NOTIFICATIONS, paramsNew.getMyAccount()));
}

/**
Expand Down Expand Up @@ -1060,7 +1060,6 @@ protected void onReceiveAfterExecutingCommand(CommandData commandData) {
public boolean isRefreshNeededAfterExecuting(CommandData commandData) {
boolean needed = super.isRefreshNeededAfterExecuting(commandData);
switch (commandData.getCommand()) {
case AUTOMATIC_UPDATE:
case FETCH_TIMELINE:
if (!getParamsLoaded().isLoaded()
|| getParamsLoaded().getTimelineType() != commandData.getTimelineType()) {
Expand Down
14 changes: 1 addition & 13 deletions app/src/main/java/org/andstatus/app/service/CommandData.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class CommandData implements Comparable<CommandData> {
private volatile boolean mManuallyLaunched = false;

/** {@link MyAccount} for this command. Invalid account if command is not Account
* specific (e.g. {@link CommandEnum#AUTOMATIC_UPDATE} which works for all accounts)
* specific e.g. {@link CommandEnum#DELETE_COMMAND}
* It holds UserId for command, which need such parameter (not only for a timeline)
*/
private final Timeline timeline;
Expand Down Expand Up @@ -149,16 +149,6 @@ private CommandData(long commandId, CommandEnum command, Timeline timeline, long
resetRetries();
}

// TODO: Get rid of this
public static CommandData forOneExecStep(CommandExecutionContext execContext) {
Bundle bundle = execContext.getCommandData().toBundle();
bundle.putString(IntentExtra.ACCOUNT_NAME.key, execContext.getMyAccount().getAccountName());
bundle.putString(IntentExtra.TIMELINE_TYPE.key, execContext.getTimelineType().save());
CommandData commandData = CommandData.fromBundle(bundle);
commandData.commandResult = execContext.getCommandData().getResult().forOneExecStep();
return commandData;
}

/**
* Used to decode command from the Intent upon receiving it
*/
Expand Down Expand Up @@ -421,7 +411,6 @@ private String toUserFriendlyForm(MyContext myContext, boolean summaryOnly) {
builder.append(trimConditionally(description, summaryOnly));
builder.append("\"");
break;
case AUTOMATIC_UPDATE:
case FETCH_TIMELINE:
if (getAccount().isValid()) {
if (getTimelineType().requiresUserToBeDefined()) {
Expand Down Expand Up @@ -499,7 +488,6 @@ public String toCommandProgress(MyContext myContext) {
public String toShortCommandName(MyContext myContext) {
StringBuilder builder = new StringBuilder();
switch (command) {
case AUTOMATIC_UPDATE:
case FETCH_TIMELINE:
builder.append(getTimelineType().getTitle(myContext.context()));
break;
Expand Down

0 comments on commit a02ab40

Please sign in to comment.