Skip to content

use unix command 'date' to change system time and date, this makes th… #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public int setTime(String ntpHostname, Bundle output) throws RemoteException {

output.putLong(OUTPUT_OFFSET, offset);

returnMessage = Utils.setTime(offset);
returnMessage = Utils.setTimestamp(System.currentTimeMillis() + offset);
} catch (Exception e) {
returnMessage = NtpSyncService.RETURN_SERVER_TIMEOUT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected void onHandleIntent(Intent intent) {

if (mData.containsKey(DATA_APPLY_DIRECTLY)) {
if (mData.getBoolean(DATA_APPLY_DIRECTLY)) {
returnMessage = Utils.setTime(offset);
returnMessage = Utils.setTimestamp(System.currentTimeMillis() + offset);
}
}
} catch (IOException e) {
Expand Down
35 changes: 35 additions & 0 deletions NTPSync/src/main/java/org/ntpsync/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import org.ntpsync.service.NtpSyncService;
import org.sufficientlysecure.rootcommands.Shell;
import org.sufficientlysecure.rootcommands.Toolbox;
import org.sufficientlysecure.rootcommands.command.SimpleCommand;
import org.sufficientlysecure.rootcommands.util.RootAccessDeniedException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.concurrent.TimeoutException;

import android.app.Activity;
Expand Down Expand Up @@ -68,6 +70,39 @@ public void onClick(DialogInterface dialog, int which) {
alert.show();
}

/**
* Sets time in Android using RootCommands library
*
* @param timestamp
* @return true if it succeeded
*/
public static int setTimestamp(long timestamp) {
try {
Shell rootShell = Shell.startRootShell();
SimpleCommand cmd = new SimpleCommand(String.format(Locale.ENGLISH, "date @%d", timestamp / 1000));
rootShell.add(cmd).waitForFinish();
rootShell.close();

if (cmd.getOutput().contains("bad date")) {
Log.d(Constants.TAG, "Error returned from date command!\n" + cmd.getOutput());
return NtpSyncService.RETURN_GENERIC_ERROR;
} else {
Log.d(Constants.TAG, "Date was set using RootCommands library!\n" + cmd.getOutput());
// it works, thus return true
return NtpSyncService.RETURN_OKAY;
}
} catch (RootAccessDeniedException e) {
Log.e(Constants.TAG, "Android is not rooted or root access was denied!", e);
return NtpSyncService.RETURN_NO_ROOT;
} catch (IOException e) {
Log.e(Constants.TAG, "IOException!", e);
return NtpSyncService.RETURN_GENERIC_ERROR;
} catch (TimeoutException e) {
Log.e(Constants.TAG, "Timeout of root command!", e);
return NtpSyncService.RETURN_GENERIC_ERROR;
}
}

/**
* Sets time in Android using RootCommands library
*
Expand Down