diff --git a/.gitignore b/.gitignore index dd5dda7485ecd373218d4f4fcc0becaa5340623b..98fff82fcc1c72d50431aa9a1546eda103607109 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ LICENSE NOTICE README.md README-wear.txt +.DS_STORE # Libraries used by the app # Can explicitly add if we want, but shouldn't do so blindly. Licenses, bloat, etc. diff --git a/accessibility/CheckableLayout/gradle/wrapper/gradle-wrapper.properties b/accessibility/CheckableLayout/gradle/wrapper/gradle-wrapper.properties index ef44a88c5288122910b920d505a8169266bda8c6..45ff7e499c089e929c052c334ea829bb2f223b28 100644 --- a/accessibility/CheckableLayout/gradle/wrapper/gradle-wrapper.properties +++ b/accessibility/CheckableLayout/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip \ No newline at end of file diff --git a/admin/AppRestrictionEnforcer/gradle/wrapper/gradle-wrapper.properties b/admin/AppRestrictionEnforcer/gradle/wrapper/gradle-wrapper.properties index 22aa0ef4b9a55a68a1f4d4cf855d99e3166f80bd..7b9b6541aabcf53a5fc00ae0b25e237aa46d72db 100644 --- a/admin/AppRestrictionEnforcer/gradle/wrapper/gradle-wrapper.properties +++ b/admin/AppRestrictionEnforcer/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip \ No newline at end of file diff --git a/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties b/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties index 819aeb4a5a8f789713b1efc0c55a7715db2a9752..ade4ceeb0f2fc3ef02d2782fa919a46708b3479b 100644 --- a/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties +++ b/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip \ No newline at end of file diff --git a/admin/BasicManagedProfile/gradle/wrapper/gradle-wrapper.properties b/admin/BasicManagedProfile/gradle/wrapper/gradle-wrapper.properties index 4da4a5808fee2512de0bcbe3cd6b2bb866234b02..86fc139ac8ce03bb1d041893a60d25222baf6c33 100644 --- a/admin/BasicManagedProfile/gradle/wrapper/gradle-wrapper.properties +++ b/admin/BasicManagedProfile/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip \ No newline at end of file diff --git a/admin/DeviceOwner/gradle/wrapper/gradle-wrapper.properties b/admin/DeviceOwner/gradle/wrapper/gradle-wrapper.properties index 6db5b796f1b107066fc624b764086338634f9262..e69441adf7634bd0115c7f09beb22a207a39b637 100644 --- a/admin/DeviceOwner/gradle/wrapper/gradle-wrapper.properties +++ b/admin/DeviceOwner/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip \ No newline at end of file diff --git a/admin/NfcProvisioning/gradle/wrapper/gradle-wrapper.properties b/admin/NfcProvisioning/gradle/wrapper/gradle-wrapper.properties index 6bf185da14204ed60327345a6611acd7909f94b9..276ce8b79f59965e735030b06ef6b82559a3488d 100644 --- a/admin/NfcProvisioning/gradle/wrapper/gradle-wrapper.properties +++ b/admin/NfcProvisioning/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip \ No newline at end of file diff --git a/background/JobScheduler/gradle/wrapper/gradle-wrapper.properties b/background/JobScheduler/gradle/wrapper/gradle-wrapper.properties index 4da4a5808fee2512de0bcbe3cd6b2bb866234b02..86fc139ac8ce03bb1d041893a60d25222baf6c33 100644 --- a/background/JobScheduler/gradle/wrapper/gradle-wrapper.properties +++ b/background/JobScheduler/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip \ No newline at end of file diff --git a/background/JobScheduler/kotlinApp/.google/packaging.yaml b/background/JobScheduler/kotlinApp/.google/packaging.yaml new file mode 100644 index 0000000000000000000000000000000000000000..536f91b3a9ede759c5bb44535126ef7d700fa33e --- /dev/null +++ b/background/JobScheduler/kotlinApp/.google/packaging.yaml @@ -0,0 +1,13 @@ + +# GOOGLE SAMPLE PACKAGING DATA +# +# This file is used by Google as part of our samples packaging process. +# End users may safely ignore this file. It has no relevance to other systems. +--- +status: PUBLISHED +technologies: [Android] +categories: [Background] +languages: [Kotlin] +solutions: [Mobile] +github: android-JobScheduler +license: apache2 diff --git a/media/MediaBrowserService/CONTRIB.md b/background/JobScheduler/kotlinApp/CONTRIB.md similarity index 100% rename from media/MediaBrowserService/CONTRIB.md rename to background/JobScheduler/kotlinApp/CONTRIB.md diff --git a/background/JobScheduler/kotlinApp/README.md b/background/JobScheduler/kotlinApp/README.md new file mode 100644 index 0000000000000000000000000000000000000000..319cf2d47b21103c5455c2fcba82c475fc21fd1f --- /dev/null +++ b/background/JobScheduler/kotlinApp/README.md @@ -0,0 +1,60 @@ + +Android JobScheduler Sample (Kotlin) +==================================== + +Demonstration of the JobScheduler API, which provides an interface for scheduling +background tasks when certain tasks apply. + +To understand how this sample works, try these different scenarios: + +- Unplug device, schedule a task that requires the device to be plugged in. Job will start when the +device is plugged in. +- Set a delay of 10 seconds and press back. The activity and service are finished but the service is +launched again in 10 seconds (logcat will show debug messages). +- Set a delay of 5 seconds and a work duration of 10 seconds. Schedule job and press the +back button. Open the activity again after 6 seconds. The activity will show the onStopTask even +though both activity and service were shut down. + +Pre-requisites +-------------- + +- Android SDK 27 +- Android Support Repository + +Getting Started +--------------- + +This sample uses the Gradle build system. To build this project, use the +"gradlew build" command or use "Import Project" in Android Studio. + +Support +------- + +- Google+ Community: https://plus.google.com/communities/105153134372062985968 +- Stack Overflow: http://stackoverflow.com/questions/tagged/android + +If you've found an error in this sample, please file an issue: +https://github.com/googlesamples/android-JobScheduler + +Patches are encouraged, and may be submitted by forking this project and +submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. + +License +------- + +Copyright 2017 The Android Open Source Project, Inc. + +Licensed to the Apache Software Foundation (ASF) under one or more contributor +license agreements. See the NOTICE file distributed with this work for +additional information regarding copyright ownership. The ASF licenses this +file to you under the Apache License, Version 2.0 (the "License"); you may not +use this file except in compliance with the License. You may obtain a copy of +the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations under +the License. diff --git a/background/JobScheduler/kotlinApp/app/build.gradle b/background/JobScheduler/kotlinApp/app/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..f3bd616c8637f8cea83df84ec46ebe9fdf976663 --- /dev/null +++ b/background/JobScheduler/kotlinApp/app/build.gradle @@ -0,0 +1,27 @@ +apply plugin: 'com.android.application' + +apply plugin: 'kotlin-android' + +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 27 + defaultConfig { + applicationId "com.example.android.jobscheduler" + minSdkVersion 21 + targetSdkVersion 27 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation "com.android.support:appcompat-v7:27.0.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" +} \ No newline at end of file diff --git a/background/JobScheduler/kotlinApp/app/src/main/AndroidManifest.xml b/background/JobScheduler/kotlinApp/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000000000000000000000000000000000000..5230d846c37d0e647987b8d1da76b9db8b676cff --- /dev/null +++ b/background/JobScheduler/kotlinApp/app/src/main/AndroidManifest.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/ActivityExtensions.kt b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/ActivityExtensions.kt new file mode 100644 index 0000000000000000000000000000000000000000..39a9d7e196230f324b3f3853821d6585f4f8138f --- /dev/null +++ b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/ActivityExtensions.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.jobscheduler + +import android.app.Activity +import android.widget.Toast + +/** + * Helper extension function for showing a [Toast] + */ +fun Activity.showToast(text: String) { + Toast.makeText(this, text, Toast.LENGTH_SHORT).show() +} \ No newline at end of file diff --git a/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/Constants.kt b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/Constants.kt new file mode 100644 index 0000000000000000000000000000000000000000..90fc00f894474462657d5fb5bd1e25028438cdf1 --- /dev/null +++ b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/Constants.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:JvmName("Constants") +@file:Suppress("PropertyName") + +package com.example.android.jobscheduler + +@JvmField val MSG_UNCOLOR_START = 0 +@JvmField val MSG_UNCOLOR_STOP = 1 +@JvmField val MSG_COLOR_START = 2 +@JvmField val MSG_COLOR_STOP = 3 + +@JvmField val MESSENGER_INTENT_KEY = "${BuildConfig.APPLICATION_ID}.MESSENGER_INTENT_KEY" +@JvmField val WORK_DURATION_KEY = "${BuildConfig.APPLICATION_ID}.WORK_DURATION_KEY" diff --git a/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/IncomingMessageHandler.kt b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/IncomingMessageHandler.kt new file mode 100644 index 0000000000000000000000000000000000000000..9d5b1148ddb7942fa901ff303d0a842dcb30a7b4 --- /dev/null +++ b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/IncomingMessageHandler.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.jobscheduler + +import android.os.Handler +import android.os.Message +import android.os.Messenger +import android.support.v4.content.ContextCompat.getColor +import android.view.View +import android.widget.TextView +import java.lang.ref.WeakReference +import java.util.concurrent.TimeUnit + +/** + * A [Handler] allows you to send messages associated with a thread. A [Messenger] + * uses this handler to communicate from [MyJobService]. It's also used to make + * the start and stop views blink for a short period of time. + */ +internal class IncomingMessageHandler(activity: MainActivity) : Handler() { + + // Prevent possible leaks with a weak reference. + private val mainActivity: WeakReference = WeakReference(activity) + + override fun handleMessage(msg: Message) { + val mainActivity = mainActivity.get() ?: return + val showStartView = mainActivity.findViewById(R.id.onstart_textview) + val showStopView = mainActivity.findViewById(R.id.onstop_textview) + when (msg.what) { + /* + * Receives callback from the service when a job has landed + * on the app. Turns on indicator and sends a message to turn it off after + * a second. + */ + MSG_COLOR_START -> { + // Start received, turn on the indicator and show text. + showStartView.setBackgroundColor(getColor(mainActivity, R.color.start_received)) + updateParamsTextView(msg.obj, "started") + sendMessageDelayed(Message.obtain(this, MSG_UNCOLOR_START), + TimeUnit.SECONDS.toMillis(1)) + } + /* + * Receives callback from the service when a job that previously landed on the + * app must stop executing. Turns on indicator and sends a message to turn it + * off after two seconds. + */ + MSG_COLOR_STOP -> { + // Stop received, turn on the indicator and show text. + showStopView.setBackgroundColor(getColor(mainActivity, R.color.stop_received)) + updateParamsTextView(msg.obj, "stopped") + sendMessageDelayed(obtainMessage(MSG_UNCOLOR_STOP), TimeUnit.SECONDS.toMillis(1)) + } + MSG_UNCOLOR_START -> { + uncolorButtonAndClearText(showStartView, mainActivity) + } + MSG_UNCOLOR_STOP -> { + uncolorButtonAndClearText(showStopView, mainActivity) + } + } + } + + private fun uncolorButtonAndClearText(textView: View, activity: MainActivity) { + textView.setBackgroundColor(getColor(activity, R.color.none_received)) + updateParamsTextView() + } + + private fun updateParamsTextView(jobId: Any? = null, action: String = "") { + val mainActivity = mainActivity.get() ?: return + val paramsTextView = mainActivity.findViewById(R.id.task_params) + if (jobId == null) { + paramsTextView.text = "" + return + } + paramsTextView.text = mainActivity.getString(R.string.job_status, jobId.toString(), action) + } +} diff --git a/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/MainActivity.kt b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/MainActivity.kt new file mode 100644 index 0000000000000000000000000000000000000000..b28d5c47d6b53c6b9f906adb1dab852f09091e1a --- /dev/null +++ b/background/JobScheduler/kotlinApp/app/src/main/java/com/example/android/jobscheduler/MainActivity.kt @@ -0,0 +1,162 @@ +/* + * Copyright 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.jobscheduler + +import android.app.Activity +import android.app.job.JobInfo +import android.app.job.JobScheduler +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.os.Messenger +import android.os.PersistableBundle +import android.util.Log +import android.widget.Button +import android.widget.CheckBox +import android.widget.EditText +import android.widget.RadioButton +import java.util.concurrent.TimeUnit + +/** + * Schedules and configures jobs to be executed by a [JobScheduler]. + * + * [MyJobService] can send messages to this via a [Messenger] + * that is sent in the Intent that starts the Service. + */ +class MainActivity : Activity() { + + lateinit private var anyConnectivityRadioButton: RadioButton + lateinit private var deadlineEditText: EditText + lateinit private var delayEditText: EditText + lateinit private var durationTimeEditText: EditText + lateinit private var requiresChargingCheckBox: CheckBox + lateinit private var requiresIdleCheckbox: CheckBox + lateinit private var wiFiConnectivityRadioButton: RadioButton + + // Handler for incoming messages from the service. + lateinit private var handler: IncomingMessageHandler + lateinit private var serviceComponent: ComponentName + private var jobId = 0 + + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.sample_main) + + anyConnectivityRadioButton = findViewById(R.id.checkbox_any) + deadlineEditText = findViewById(R.id.deadline_time) + delayEditText = findViewById(R.id.delay_time) + durationTimeEditText = findViewById(R.id.duration_time) + requiresChargingCheckBox = findViewById(R.id.checkbox_charging) + requiresIdleCheckbox = findViewById(R.id.checkbox_idle) + wiFiConnectivityRadioButton = findViewById(R.id.checkbox_unmetered) + + handler = IncomingMessageHandler(this) + serviceComponent = ComponentName(this, MyJobService::class.java) + + findViewById