diff --git a/Week_09_Services_and_wearables/.md_images/notification_phone.png b/Week_09_Services_and_wearables/.md_images/notification_phone.png new file mode 100644 index 0000000..3381c7a Binary files /dev/null and b/Week_09_Services_and_wearables/.md_images/notification_phone.png differ diff --git a/Week_09_Services_and_wearables/MyWearables copy/.gitignore b/Week_09_Services_and_wearables/MyWearables copy/.gitignore new file mode 100644 index 0000000..39fb081 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/Week_09_Services_and_wearables/MyWearables copy/.idea/compiler.xml b/Week_09_Services_and_wearables/MyWearables copy/.idea/compiler.xml new file mode 100644 index 0000000..32b8ce7 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/.idea/copyright/profiles_settings.xml b/Week_09_Services_and_wearables/MyWearables copy/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..7d61b5c --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/.idea/gradle.xml b/Week_09_Services_and_wearables/MyWearables copy/.idea/gradle.xml new file mode 100644 index 0000000..c3b1da4 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/.idea/misc.xml b/Week_09_Services_and_wearables/MyWearables copy/.idea/misc.xml new file mode 100644 index 0000000..184803f --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/.idea/misc.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/.idea/modules.xml b/Week_09_Services_and_wearables/MyWearables copy/.idea/modules.xml new file mode 100644 index 0000000..b94c244 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/.idea/runConfigurations.xml b/Week_09_Services_and_wearables/MyWearables copy/.idea/runConfigurations.xml new file mode 100644 index 0000000..9b6e38d --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/.gitignore b/Week_09_Services_and_wearables/MyWearables copy/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/build.gradle b/Week_09_Services_and_wearables/MyWearables copy/app/build.gradle new file mode 100644 index 0000000..4c2cbe4 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/build.gradle @@ -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' +} diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/proguard-rules.pro b/Week_09_Services_and_wearables/MyWearables copy/app/proguard-rules.pro new file mode 100644 index 0000000..cce7213 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/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/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 *; +#} diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/androidTest/java/com/example/jianhuayang/mywearables/ExampleInstrumentedTest.java b/Week_09_Services_and_wearables/MyWearables copy/app/src/androidTest/java/com/example/jianhuayang/mywearables/ExampleInstrumentedTest.java new file mode 100644 index 0000000..67c500d --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/androidTest/java/com/example/jianhuayang/mywearables/ExampleInstrumentedTest.java @@ -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 Testing documentation + */ +@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()); + } +} diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/main/AndroidManifest.xml b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..fb4843c --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/AndroidManifest.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/CountingReceiver.java b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/CountingReceiver.java new file mode 100644 index 0000000..3951ffc --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/CountingReceiver.java @@ -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); + } + +} diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/CountingService.java b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/CountingService.java new file mode 100644 index 0000000..39ae1a1 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/CountingService.java @@ -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"); + + } +} diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/DisplayActivity.java b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/DisplayActivity.java new file mode 100644 index 0000000..0258027 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/DisplayActivity.java @@ -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)); + } + } +} diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/MainActivity.java b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/MainActivity.java new file mode 100644 index 0000000..be541c5 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/java/com/example/jianhuayang/mywearables/MainActivity.java @@ -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"); + } +} diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/main/res/layout/activity_display.xml b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/res/layout/activity_display.xml new file mode 100644 index 0000000..9d1e4d1 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/res/layout/activity_display.xml @@ -0,0 +1,20 @@ + + + + + + diff --git a/Week_09_Services_and_wearables/MyWearables copy/app/src/main/res/layout/activity_main.xml b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..5518d18 --- /dev/null +++ b/Week_09_Services_and_wearables/MyWearables copy/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,24 @@ + + + +