diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index 294714d3e..a75f19fdb 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -1253,6 +1253,17 @@ + + + + + + + + + diff --git a/samples/ApiDemos/res/layout/media_content_observer.xml b/samples/ApiDemos/res/layout/media_content_observer.xml new file mode 100644 index 000000000..63292e6f2 --- /dev/null +++ b/samples/ApiDemos/res/layout/media_content_observer.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index 92e071947..ef4e2a061 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -469,6 +469,8 @@ Content/Provider/Changed Contacts + Content/Provider/Media Content Observer + Content/Packages/Install Apk Send IR diff --git a/samples/ApiDemos/src/com/example/android/apis/content/MediaContentJob.java b/samples/ApiDemos/src/com/example/android/apis/content/MediaContentJob.java new file mode 100644 index 000000000..4c788e06d --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/content/MediaContentJob.java @@ -0,0 +1,114 @@ +/** + * Copyright (c) 2016, 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.apis.content; + +import android.app.job.JobInfo; +import android.app.job.JobParameters; +import android.app.job.JobScheduler; +import android.app.job.JobService; +import android.content.ComponentName; +import android.content.Context; +import android.net.Uri; +import android.os.Handler; +import android.provider.MediaStore; +import android.widget.Toast; + +import com.example.android.apis.R; + +import java.util.List; + +/** + * Stub job to execute when there is a change to any media: content URI. + */ +public class MediaContentJob extends JobService { + static final Uri MEDIA_URI = Uri.parse("content://" + MediaStore.AUTHORITY + "/"); + + final Handler mHandler = new Handler(); + final Runnable mWorker = new Runnable() { + @Override public void run() { + scheduleJob(MediaContentJob.this); + jobFinished(mRunningParams, false); + } + }; + + JobParameters mRunningParams; + + public static void scheduleJob(Context context) { + JobScheduler js = context.getSystemService(JobScheduler.class); + JobInfo.Builder builder = new JobInfo.Builder(R.id.schedule_job, + new ComponentName(context, MediaContentJob.class)); + builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MEDIA_URI, + JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)); + js.schedule(builder.build()); + } + + public static boolean isScheduled(Context context) { + JobScheduler js = context.getSystemService(JobScheduler.class); + List jobs = js.getAllPendingJobs(); + if (jobs == null) { + return false; + } + for (int i=0; i