diff --git a/NTPSync/src/main/java/org/ntpsync/service/NtpSyncRemoteService.java b/NTPSync/src/main/java/org/ntpsync/service/NtpSyncRemoteService.java index 2cee7cc..25dbbd1 100644 --- a/NTPSync/src/main/java/org/ntpsync/service/NtpSyncRemoteService.java +++ b/NTPSync/src/main/java/org/ntpsync/service/NtpSyncRemoteService.java @@ -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; } diff --git a/NTPSync/src/main/java/org/ntpsync/service/NtpSyncService.java b/NTPSync/src/main/java/org/ntpsync/service/NtpSyncService.java index b5f4b93..89a4307 100644 --- a/NTPSync/src/main/java/org/ntpsync/service/NtpSyncService.java +++ b/NTPSync/src/main/java/org/ntpsync/service/NtpSyncService.java @@ -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) { diff --git a/NTPSync/src/main/java/org/ntpsync/util/Utils.java b/NTPSync/src/main/java/org/ntpsync/util/Utils.java index e2058ac..b9cc2e1 100644 --- a/NTPSync/src/main/java/org/ntpsync/util/Utils.java +++ b/NTPSync/src/main/java/org/ntpsync/util/Utils.java @@ -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; @@ -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 *