Skip to content
Permalink
Browse files
week9 10%
  • Loading branch information
Jianhua Yang committed Nov 27, 2017
1 parent 3075dfd commit 632b39bd7fb847c0e7bb4efdf1a6323bdbd04e0c
Show file tree
Hide file tree
Showing 87 changed files with 1,806 additions and 7 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1 @@
/build
@@ -0,0 +1,29 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.example.jianhuayang.mywearables"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
}
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/jianhuayang/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 *;
#}
@@ -0,0 +1,26 @@
package com.example.jianhuayang.mywearables;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.example.jianhuayang.mywearables", appContext.getPackageName());
}
}
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jianhuayang.mywearables">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".CountingService"
android:enabled="true"
android:exported="false"
/>
<receiver
android:name=".CountingReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.example.jianhuayang.mywearables.BROADCAST"/>
</intent-filter>
</receiver>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DisplayActivity">
</activity>
</application>

</manifest>
@@ -0,0 +1,29 @@
package com.example.jianhuayang.mywearables;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
* Created by jianhuayang on 25/11/2016.
*/

public class CountingReceiver extends BroadcastReceiver {
public CountingReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
Log.d(MainActivity.DEBUG_KEY, "on receive");
String timeElapsed = intent.getStringExtra(CountingService.REPORT_KEY);
Log.d(MainActivity.DEBUG_KEY, "time elapsed: " + timeElapsed);

Intent intentNew = new Intent(context, DisplayActivity.class);
intentNew.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentNew.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentNew.putExtra(CountingService.REPORT_KEY, timeElapsed);
context.startActivity(intentNew);
}

}
@@ -0,0 +1,73 @@
package com.example.jianhuayang.mywearables;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;

/**
* Created by jianhuayang on 25/11/2016.
*/

public class CountingService extends IntentService {

public static final String REPORT_KEY = "REPORT_KEY";
public static final String INTENT_KEY = "com.example.jianhuayang.mywearables.BROADCAST";

public CountingService() {
super("BackgroundCounting");
}

@Override
protected void onHandleIntent(Intent intent) {

int count = 0;
while (count < 10) {
synchronized (this) {
try {
wait(1000);
count++;
Log.d(MainActivity.DEBUG_KEY, Integer.toString(count));

// send info using intent
// Intent intentNew = new Intent(getBaseContext(), DisplayActivity.class);
// intentNew.putExtra(REPORT_KEY, Integer.toString(count));
// intentNew.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intentNew.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// getBaseContext().startActivity(intentNew);

// Intent localIntent = new Intent();
// localIntent.setAction(INTENT_KEY);
// localIntent.putExtra(REPORT_KEY, Integer.toString(count));
// sendBroadcast(localIntent);
// Log.d(MainActivity.DEBUG_KEY, "broadcasted");

NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Wearables")
.setContentText("Time elapsed: " + Integer.toString(count) + " seconds.");
Intent resultIntent = new Intent(this, DisplayActivity.class);
resultIntent.putExtra(REPORT_KEY, Integer.toString(count));

TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(DisplayActivity.class);
taskStackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(123123, builder.build());
} catch (Exception e) {
}
}
}
Log.d(MainActivity.DEBUG_KEY, "service finished");

}
}
@@ -0,0 +1,20 @@
package com.example.jianhuayang.mywearables;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class DisplayActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
if (getIntent() != null) {
Intent intent = getIntent();
TextView textView = (TextView) findViewById(R.id.display);
textView.setText("Time elapsed (seconds):\n" + intent.getStringExtra(CountingService.REPORT_KEY));
}
}
}
@@ -0,0 +1,22 @@
package com.example.jianhuayang.mywearables;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

public static final String DEBUG_KEY = "DEBUG_KEY";
public void onStartClick(View v) {
startService(new Intent(this, CountingService.class));
Log.d(DEBUG_KEY, "service started");
}
}

0 comments on commit 632b39b

Please sign in to comment.