Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ebelair committed Mar 2, 2016
0 parents commit 6be57bf
Show file tree
Hide file tree
Showing 29 changed files with 753 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
@@ -0,0 +1,13 @@
# Mac OS X
.DS_Store

# Android project
.gradle
.android
local.properties
.idea/
*.iml
caches/
native/
build/
captures/
26 changes: 26 additions & 0 deletions LICENSE.md
@@ -0,0 +1,26 @@
Copyright (c) 2013-2015, Mirego
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mirego nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
51 changes: 51 additions & 0 deletions README.md
@@ -0,0 +1,51 @@
# CS Games 2016 - Sample Android App

This project will guide you setting up your environment to build and run a sample Android application on your local machine.

## Prerequisites

Make sure you have the following software installed before beginning:

- Latest version of Android Studio (1.5.1)
- Recent version of the Android SDK (at least API 21)

You can download these from the [Android Developer website](http://developer.android.com/sdk/index.html).

> **NOTE:** If you have a Mac computer running OS X 10.10 or later, you may also be interested in our [Sample iOS app](https://github.com/mirego/csgames16-sample-ios/), which uses Xcode and the latest iOS SDK.
## Getting started

First, clone the project from Github:

```
git clone git@github.com:mirego/csgames16-sample-android.git
```

Then, in Android Studio:

- Select **Import project (Eclipse, ADT, Gradle, etc.)** in the Welcome Screen, go find the repository you just cloned, and click **OK**.
- Once the project is open, click on **Sync Project with Gradle Files** in the main toolbar (or navigate to `Tools -> Android` in the application menu and select the same option).

<p align="center"><img src="https://cloud.githubusercontent.com/assets/4378424/13450169/9f925920-e000-11e5-999a-464b9949ee9a.png" width="199"></p>

Once you see a `BUILD SUCCESSFUL` notice in the Gradle Console, your environment should be ready to build and run the project.

## Building the project

The project should have already been configured as an Android project in Android Studio, therefore you should see a target named `app` in the main toolbar, with **Play** and **Debug** buttons on its right.

Press on the **Debug** icon, and if you don't already have one, [create a new Android Virtual Device](http://developer.android.com/tools/devices/managing-avds.html), then select it to run the project.

Once the app appears running in your Virtual Device, your environment is ready for the competition.

## License

This sample app is © 2016 [Mirego](http://www.mirego.com) and may be freely
distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause).
See the [`LICENSE.md`](https://github.com/mirego/csgames16-sample-ios/blob/master/LICENSE.md) file.

## About Mirego

[Mirego](http://mirego.com) is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of [talented people](http://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://mirego.org).

We also [love open-source software](http://open.mirego.com) and we try to give back to the community as much as we can.
1 change: 1 addition & 0 deletions app/.gitignore
@@ -0,0 +1 @@
/build
28 changes: 28 additions & 0 deletions app/build.gradle
@@ -0,0 +1,28 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.mirego.sampleapp"
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/ebelair/Library/Android/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 *;
#}
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mirego.sampleapp">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".view.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
17 changes: 17 additions & 0 deletions app/src/main/java/com/mirego/sampleapp/data/DataSource.java
@@ -0,0 +1,17 @@
package com.mirego.sampleapp.data;

import java.util.Date;

public interface DataSource {

interface Observer {
void didRefresh();
}

void refreshData();

Date getCurrentDate();

void registerObserver(Observer observer);
void unregisterObserver(Observer observer);
}
76 changes: 76 additions & 0 deletions app/src/main/java/com/mirego/sampleapp/data/DataSourceImpl.java
@@ -0,0 +1,76 @@
package com.mirego.sampleapp.data;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DataSourceImpl implements DataSource {

private final String TIME_API_URL = "http://www.timeapi.org/utc/now";
private final String TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZZZZ";

private DateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);
private OkHttpClient client = new OkHttpClient();

private Date currentDate = null;
private List<Observer> observerList = new ArrayList<>();

@Override
public void refreshData() {
Request request = new Request.Builder()
.url(TIME_API_URL)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
currentDate = null;
notifyObservers();
}

@Override
public void onResponse(Call call, Response response) throws IOException {
try {
currentDate = dateFormat.parse(response.body().string());
} catch(ParseException e) {
currentDate = null;
}
notifyObservers();
}
});
}

@Override
public Date getCurrentDate() {
return currentDate;
}

@Override
public void registerObserver(Observer observer)
{
observerList.add(observer);
}

@Override
public void unregisterObserver(Observer observer)
{
observerList.remove(observer);
}

private void notifyObservers()
{
for (Observer observer : observerList) {
observer.didRefresh();
}
}
}
100 changes: 100 additions & 0 deletions app/src/main/java/com/mirego/sampleapp/view/MainActivity.java
@@ -0,0 +1,100 @@
package com.mirego.sampleapp.view;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.TextView;

import com.mirego.sampleapp.R;
import com.mirego.sampleapp.data.DataSource;
import com.mirego.sampleapp.data.DataSourceImpl;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends Activity {

private final long TIMER_INTERVAL = 1000;

private DataSource dataSource = new DataSourceImpl();
private Timer timer = new Timer();

@Bind(R.id.week_day_text_view)
TextView weekDayTextView;

@Bind(R.id.day_text_view)
TextView dayTextView;

@Bind(R.id.month_text_view)
TextView monthTextView;

@Bind(R.id.time_text_view)
TextView timeTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ButterKnife.bind(this);

dataSource.registerObserver(new DataSource.Observer() {
@Override
public void didRefresh() {
runOnUiThread(new Runnable() {
@Override
public void run() {
displayDate(dataSource.getCurrentDate());
}
});
}
});

startTimer();
}

@Override
public void onPause() {
super.onPause();
cancelTimer();
}

@Override
public void onResume() {
super.onResume();
startTimer();
}

@Override
public void onDestroy() {
super.onDestroy();
cancelTimer();
}

private void startTimer() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
dataSource.refreshData();
}
}, 0, TIMER_INTERVAL);
}

private void cancelTimer() {
timer.cancel();
timer.purge();
}

private void displayDate(Date date) {
weekDayTextView.setText(DateFormat.format("EEEE", date));
dayTextView.setText(DateFormat.format("dd", date));
monthTextView.setText(DateFormat.format("MMMM", date));
timeTextView.setText(DateFormat.format("HH'h' mm'm' ss's'", date));
}
}

0 comments on commit 6be57bf

Please sign in to comment.