-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All mandatory requests are done. Just optional feature need more time…
… to make it's look and feel better
- Loading branch information
0 parents
commit b33ef05
Showing
28 changed files
with
950 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Created by .gitignore support plugin (hsz.mobi) | ||
|
||
### Android template | ||
# Built application files | ||
*.apk | ||
*.ap_ | ||
|
||
# Files for the Dalvik VM | ||
*.dex | ||
|
||
# Java class files | ||
*.class | ||
|
||
# Generated files | ||
bin/ | ||
gen/ | ||
|
||
# Gradle files | ||
.gradle/ | ||
build/ | ||
|
||
# Local configuration file (sdk path, etc) | ||
local.properties | ||
|
||
# Proguard folder generated by Eclipse | ||
proguard/ | ||
|
||
# Log Files | ||
*.log | ||
|
||
# Eclipse project files | ||
.classpath | ||
.project | ||
|
||
# Intellij project files | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 20 | ||
buildToolsVersion "20.0.0" | ||
|
||
packagingOptions { | ||
exclude 'META-INF/LICENSE.txt' | ||
} | ||
|
||
defaultConfig { | ||
applicationId "ntpclock.kamalan.com.app" | ||
minSdkVersion 15 | ||
targetSdkVersion 20 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
buildTypes { | ||
release { | ||
runProguard false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(dir: 'libs', include: ['*.jar']) | ||
compile 'com.squareup.retrofit:retrofit:1.6.1' | ||
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' | ||
compile 'com.squareup.okhttp:okhttp:2.0.0' | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in F:\Android SDK\sdk/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
13 changes: 13 additions & 0 deletions
13
app/src/androidTest/java/ntpclock/kamalan/com/app/ApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ntpclock.kamalan.com.app; | ||
|
||
import android.app.Application; | ||
import android.test.ApplicationTestCase; | ||
|
||
/** | ||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> | ||
*/ | ||
public class ApplicationTest extends ApplicationTestCase<Application> { | ||
public ApplicationTest() { | ||
super(Application.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="ntpclock.kamalan.com.app" > | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@drawable/ic_launcher" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme" > | ||
<activity | ||
android:name=".MainActivity" | ||
android:label="@string/app_name" > | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
189 changes: 189 additions & 0 deletions
189
app/src/main/java/ntpclock/kamalan/com/app/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package ntpclock.kamalan.com.app; | ||
|
||
import android.app.Activity; | ||
import android.os.AsyncTask; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.util.Log; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.view.ViewGroup; | ||
import android.widget.FrameLayout; | ||
import android.widget.RelativeLayout; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import org.apache.commons.net.ntp.NTPUDPClient; | ||
import org.apache.commons.net.ntp.TimeInfo; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.SocketException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
import ntpclock.kamalan.com.utility.InternetConnection; | ||
import ntpclock.kamalan.com.utility.NTPClient; | ||
import ntpclock.kamalan.com.widget.MyAnalogClock; | ||
|
||
public class MainActivity extends Activity { | ||
|
||
private static final String TAG = "MainActivity"; | ||
private static final String SERVER_ADDR = "0.asia.pool.ntp.org"; | ||
private static final long UPDATE_INTERVALS = 10 * 60 * 1000; | ||
|
||
private Handler mHandler = new Handler(); | ||
private Runnable mRunnable = new Runnable() { | ||
@Override | ||
public void run() { | ||
displayCurrentTime(); | ||
displayRemainingTime(); | ||
displayAnalogClock(); | ||
mHandler.postDelayed(mRunnable, 1000); | ||
} | ||
}; | ||
|
||
private Date currentTime; | ||
private Date nextUpdateTime; | ||
|
||
private TextView mTimeDisplay; | ||
private TextView mTimeRemaining; | ||
private RelativeLayout mRelativeLayout; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
mTimeDisplay = (TextView) findViewById(R.id.tvTimeDisplay); | ||
mTimeRemaining = (TextView) findViewById(R.id.tvRemainingTime); | ||
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); | ||
|
||
getTimeFromNTPServer(); | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
|
||
checkInternetStatus(); | ||
} | ||
|
||
protected void onStop() { | ||
super.onStop(); | ||
|
||
mHandler.removeCallbacks(mRunnable); | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
// Inflate the menu; this adds items to the action bar if it is present. | ||
getMenuInflater().inflate(R.menu.main, menu); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
// Handle action bar item clicks here. The action bar will | ||
// automatically handle clicks on the Home/Up button, so long | ||
// as you specify a parent activity in AndroidManifest.xml. | ||
int id = item.getItemId(); | ||
if (id == R.id.action_sync) { | ||
getTimeFromNTPServer(); | ||
return true; | ||
} | ||
|
||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
private void getTimeFromNTPServer() { | ||
if(InternetConnection.isAvailable(MainActivity.this)) | ||
new getNTPTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); | ||
} | ||
|
||
private void checkInternetStatus() { | ||
if(!InternetConnection.isAvailable(MainActivity.this)) | ||
Toast.makeText(MainActivity.this, "Internet connection not found!", Toast.LENGTH_LONG).show(); | ||
} | ||
|
||
private void displayCurrentTime() { | ||
if (currentTime.getTime() >= nextUpdateTime.getTime()) { | ||
getTimeFromNTPServer(); | ||
return; | ||
} | ||
|
||
long newTime = currentTime.getTime(); | ||
newTime += 1000; // reduce one seconds | ||
currentTime = new Date(newTime); | ||
String newDate = new SimpleDateFormat("EEE dd MMM yyyy hh:mm:ss").format(currentTime); | ||
mTimeDisplay.setText("Current network time:\n" + newDate); | ||
} | ||
|
||
private void displayRemainingTime() { | ||
long remainingTime = nextUpdateTime.getTime() - currentTime.getTime(); | ||
Date time = new Date(remainingTime); | ||
String newDate = new SimpleDateFormat("mm:ss").format(time); | ||
mTimeRemaining.setText("To next update:\n" + newDate); | ||
} | ||
|
||
private void displayAnalogClock() { | ||
mRelativeLayout.removeAllViews(); | ||
mRelativeLayout.addView(new MyAnalogClock(MainActivity.this, | ||
mRelativeLayout.getMeasuredWidth()/2, | ||
mRelativeLayout.getMeasuredHeight()/2, | ||
currentTime)); | ||
} | ||
|
||
/************************ | ||
* Async Task Class * | ||
************************/ | ||
private class getNTPTask extends AsyncTask<Void, Void, Date> { | ||
|
||
@Override | ||
protected void onPreExecute() { | ||
Log.d(TAG, "getNTPTask is about to start..."); | ||
mHandler.removeCallbacks(mRunnable); | ||
} | ||
|
||
@Override | ||
protected Date doInBackground(Void... params) { | ||
Date date = null; | ||
|
||
NTPUDPClient client = new NTPUDPClient(); | ||
// We want to timeout if a response takes longer than 10 seconds | ||
client.setDefaultTimeout(15000); | ||
try { | ||
client.open(); | ||
try { | ||
InetAddress hostAddr = InetAddress.getByName(SERVER_ADDR); | ||
System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress()); | ||
TimeInfo info = client.getTime(hostAddr); | ||
date = NTPClient.processResponse(info); | ||
} catch (IOException ioe) { | ||
ioe.printStackTrace(); | ||
} | ||
} catch (SocketException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
client.close(); | ||
} | ||
|
||
return date; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Date date) { | ||
Log.d(TAG, "getNTPTask finished its task."); | ||
|
||
if(date != null) { | ||
currentTime = date; | ||
long time = date.getTime(); | ||
time += UPDATE_INTERVALS; | ||
nextUpdateTime = new Date(time); | ||
mHandler.post(mRunnable); | ||
} else | ||
Toast.makeText(MainActivity.this, "Error! Couldn't get the time", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
app/src/main/java/ntpclock/kamalan/com/utility/InternetConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ntpclock.kamalan.com.utility; | ||
|
||
import android.content.Context; | ||
import android.net.ConnectivityManager; | ||
import android.net.NetworkInfo; | ||
import android.util.Log; | ||
|
||
/** | ||
* Created by Hesam on 16/07/14 | ||
*/ | ||
public class InternetConnection { | ||
private static String TAG = "InternetConnection"; | ||
|
||
private static int internetStatus = -1; | ||
private static String internetType = null; | ||
|
||
|
||
public static boolean isAvailable(Context context) { | ||
boolean found = false; | ||
|
||
try { | ||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | ||
NetworkInfo netInfo = cm.getActiveNetworkInfo(); | ||
if (netInfo != null && netInfo.isConnected()) { | ||
found = true; | ||
internetStatus = 0; | ||
} | ||
|
||
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); | ||
NetworkInfo _3g = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); | ||
|
||
if (wifi.isConnected()) | ||
internetType = "WIFI"; | ||
|
||
if (_3g.isConnected()) | ||
internetType = "3G"; | ||
|
||
} catch (Exception e) { | ||
Log.e("CheckConnectivity Exception", e.getMessage(), e); | ||
} | ||
|
||
if(!found) | ||
Log.e(TAG, "Internet Connection not found."); | ||
|
||
return found; | ||
} | ||
|
||
public static int getInternetStatus() { | ||
return internetStatus; | ||
} | ||
|
||
public static String getInternetType() { | ||
return internetType; | ||
} | ||
} |
Oops, something went wrong.