Time Tracker in Android Independent of the System time - android

I've tried to google around but I still can't find the best answer.
All I want is very simple, I am new to Android and I am developing a Game which will give some daily Bonuses to the user, if he plays Everyday.
My Code is:
date = new Date();
dateFormate = new SimpleDateFormat("yyyy/MM/dd");
dateString = dateFormate.format(date);
if(!sharedPreferences.getString("date").equals(dateString))
{
sharedPreferences.putString("date", dateString).commit();
... Here I give Bonus to user
}
BUT the Problem is, This code is dependent on the System Date. User can Easily change the date and can get its daily Reward.
I want to track 24 hours time once he starts playing The game.
Can Anyone Help me Please.
This would be much Appreciated.

If your app has internet access you could get time from your own server, rather than user time.
You could listen to Intent.ACTION_TIME_CHANGED and sync with server time only if you have recieved this intent. You wont be syncing with server unless user changes time manually.

instead of comparing that date has changed you can check if
date is bigger than previos one: asssign bonus and remeber date
date is smaller than previos one: user is cheating-remove all bonuses and do not update old date
this solution is of course not bullet proof but more secure than you previos one

Related

How to protect android game with points award by time interval from date changing cheating?

I'm developing an android game that has points. There are some ways to get points and the one of them is to open the game every day. I thought of two ways to implement this but the both have security problems.
The first way: system date
The app's service gets current system date and checks if bonus was awarded for this date; if it wasn't then award and write this date to the database (suppose this database is encrypted and there is no way to change the data inside it).
The problem: cheater can change system date and get points again and again.
Possible ways of protection:
Fix installation date. If current system date is less that installation date it's a cheater, don't award points. Weakness: cheater can change date to the future not to the past.
The same as first but additionally we can check that installation date is greater that app release date (hardcoded, yes). Weakness: first not but growing every day.
Check date using the internet. If current system date is less or greater than internet date by more than 24 hours it's a cheater, don't award points. Weakness: cheater can disable the internet.
The second way: timer
Suppose that we want to make bonus every day at 00:00. When user starts the game first time (suppose at 20:00) calculate how many time left before the bonus time (4 hours). Set timer for this time (4 hours). When the timer goes off award points and start the new one for 24 hours and then similarly.
The problem: device can be rebooted and the timer will be destroyed. So after reboot we need to repeat algorithm as the first time: calculate how many time left before the bonus time and start the timer. Cheater can change time to 23:55 and reboot the device and get points; and than do it again and again.
The third way: timer and database
The same as the second way but additionally timer writes time left to database every minute (for example) and after reboot restore this value.
The problem: I see no weakness but it awards points every 24 hours but not every day.
That's all...
By the way: I've tested the first way of cheating with some games and it works. But some others are protected from this.
I would be happy to hear any ideas or advice. And thanks for your time.

Android: Actual Date and Device Date

This is my first time to ask here on Stackoverflow and I am apparently having a hard time on this one. How could I identify if the user changed the device's date in order to activate a date-based code actions?
I really need to compare the actual date and device date without internet connection.
For example:
When I click this button, it would only open in August 29. Changing the date would allow me to access the function of the button.
You can use a Network Time Protocol. Which provides the network time and date so it can't be tricked by the user changing the phone date.
Google has one open source at: This link
EDIT
Which provides this code as sample:
SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
}
where it puts time.foo.com you should put time.google.com. You might need a timeout in milliseconds to add to the requestTime(host,timeout_millis) method.
And for NTP server you can use
time.google.com
Provided Here
If you can make sure that when your app is installed it has correct time then, you may implement a listener to know manual clock change and then do what you want to do. Reference: Is there a way to detect when the user has changed the clock time on their device?.
There are 2 more options, one to get time using GPS and other is to get time from Network. Not too sure about the network, it is something NTP related stuff will explore when I get a chance. Let me know about your implementation.

Get datetime/timestamp from a website/url in android

here is what i'am trying to achieve, either one of the two would be ok
Get the date and time from a specific url/website like "www.mywebsite.com"
Get the date and time from my MSSQL Server
either one of the two would be ok, because all i need is to get the same time from the website/server's end, because i'am making an APP Version for the Mobile Website i'am making and i want to show a Clock or Date&Time Reminder in the App
i have no codes working as of now, i tried searching online in search engines and also in stackoverflow but i can't seem to find what i'am looking for, either i'am using the wrong words to search for it or there really isn't any way
the reason why i need the date is because i'am checking when the records inside the database was created and verifying how much time has passed since it was created, it like comparing (CreationDateTime & CurrentDateTime)
i can successfully Display how many hours have passed, i just need the CURRENT PROPER & ACCURATE DATE & TIME of the server :)
I can't help but think this is an XY problem, or at least close to it.
Your client device knows what time it is, because it stays in sync via NTP and whatever network it's attached to (and we already know you're attached to a network because otherwise, this whole thing falls apart). And it knows (or should know) what timezone it's in, so you can translate that to UTC or whatever alternative timezone you might need.
Your server(s) should also be keeping time synced via NTP, so drift between the client and server should be sub-second at worst.
IOW, the time (when corrected to UTC, which all date/time libraries I've used have a method built in to do) will almost always match between your client and server, so why bother with the complexity of "pushing" the current time from server to client?
I'm not seeing why you'd need to do this at all. Have the server tell the client when the reminder is set for, then let the client work out the rest. Anything else would be excessively chatty over the network.
To get the date in the format below, just convert todays date to a string
--07/06/2017
SELECT convert(varchar(10), getdate(), 103)
i have found an alternative answer that works 100% in getting date and time from SERVER instead of URL
SELECT GETDATE() AS CurrentDateAndTime
GETDATE()

Making a timestamp from time calculated from elapsedRealtime and time gotten from server

This is more of a "is this valid" type of question than "how do I do it", since my coworkers want a second opinion on it. We want to have an accurate timestamp of the time user takes a picture using our application, and the stamp should be something that our users can't influence. Since we want our application to work even though there's no internet connection, just asking the time from a server won't work.
I've currently done this so that the application asks a timestamp from the server, and at the same time takes note of what time Android's elapsedRealtime() clock has at that point. When user takes a picture, the software checks the elapsedRealtime() again, and then calculates the timespan between the two points of time saved from elapsedRealtime(). That timespan is then added to the timestamp gotten from the server, so that we'd have the time that represents the user's current time.
In the abstract level, does this sound like a valid solution that gives accurate time everytime? Are there other solutions to do this?
EDIT:
A requirement I forgot to mention: the pictures can't be lost even if the phone is turned off before sending them to the server, nor should the timestamp lose validity because of that.
Thanks in advance,
Xevas
If it is absolutely imperative to get this time correct, you could start a timer when the user takes the first picture and then when you get internet access, check time on the server and calculate the time when the picture was taken by subtracting the time elapsed since the timer was turned on from the current time.

One month restiction for application free use

Requirement:
In my application I want to allow user to free use all features for one month from the day of application's first run. For this I have store first run date in application database and compare first run date with current date every time when application launch.
Implementation:
For getting current date we have several function like Calendar.getInstance(); Date date=new Date(); these function will return the date\datetime of the standard "wall" clock (time and date). According to document Which can be set by the user or the phone network.
The Problem:
Every thing seems to be work fine but what if user change date from Setting and set it to past date. For example user have first run application on 7 June 2013 so after 6 July 2013 application must show that he have to purchase subscription, but if user change date of device back to 30 June etc. then the restriction will not work.
So is there any why to implement it correctly?
Is there any why to get actual time of device that is not be editable by user?
Edit:
Application can work offline, so user can turn off internet connectivity.
How about storing two dates - the date the app was first used and the date it was last used. 30 minus the difference between the two dates will give you the number of days remaining and if the current date is earlier than the last used date you know that they have changed the date back.
Just an idea - not tried it but in theory it should work.
Store the first day it was used and the last day it was used, and listen to the ACTION_TIME_CHANGED intent. Also, try using an AlarmManager.
Or if you have network, store it in a database off the phone.
But you probably need to ask yourself if someone really would change the date of their phone just to use your app. It would seriously mess up a lot of things for them: alarms, calenders, syncing to other services and so on. I don't think it is a problem in reality.
If it's a possibility, you could use a one-shot GPS request to get a time value. The user can't change the GPS time that it receives. Adding the permission is a pain if you don't need it, but this would prevent bad behavior caused by the user mucking with the date/time.
Event(APP_INSTALL) -> storeOnServer(INSTALL_TIME_KEY,getTimeFromServer());
Event(APP_LAUNCH) -> IF(getTimeFromServer -getStoredTime(INSTALL_TIME_KEY) >30)
-> showTrialPeriodEndedAlert()
-> quitApp();

Categories

Resources