1 /** 2 * Copyright (c) 2016, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.content; 18 19 import android.Manifest; 20 import android.app.Activity; 21 import android.content.pm.PackageManager; 22 import android.database.ContentObserver; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.util.Log; 27 import android.view.View; 28 import android.widget.TextView; 29 30 import com.example.android.apis.R; 31 32 public class MediaContentObserver extends Activity { 33 public static final int REQ_PHOTOS_PERM = 1; 34 35 ContentObserver mContentObserver; 36 View mScheduleMediaJob; 37 View mCancelMediaJob; 38 View mSchedulePhotosJob; 39 View mCancelPhotosJob; 40 TextView mDataText; 41 42 @Override onCreate(Bundle savedInstanceState)43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 46 mContentObserver = new ContentObserver(new Handler()) { 47 public void onChange(boolean selfChange, Uri uri) { 48 mDataText.append(uri.toString()); 49 mDataText.append("\n"); 50 } 51 }; 52 53 Log.d("foo", "Observing: " + MediaContentJob.MEDIA_URI); 54 55 // See res/any/layout/resources.xml for this view layout definition. 56 setContentView(R.layout.media_content_observer); 57 58 mScheduleMediaJob = findViewById(R.id.schedule_media_job); 59 mCancelMediaJob = findViewById(R.id.cancel_media_job); 60 mSchedulePhotosJob = findViewById(R.id.schedule_photos_job); 61 mCancelPhotosJob = findViewById(R.id.cancel_photos_job); 62 mDataText = (TextView)findViewById(R.id.changes_text); 63 64 mScheduleMediaJob.setOnClickListener(new View.OnClickListener() { 65 @Override 66 public void onClick(View v) { 67 MediaContentJob.scheduleJob(MediaContentObserver.this); 68 updateButtons(); 69 } 70 }); 71 mCancelMediaJob.setOnClickListener(new View.OnClickListener() { 72 @Override 73 public void onClick(View v) { 74 MediaContentJob.cancelJob(MediaContentObserver.this); 75 updateButtons(); 76 } 77 }); 78 mSchedulePhotosJob.setOnClickListener(new View.OnClickListener() { 79 @Override 80 public void onClick(View v) { 81 if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) 82 != PackageManager.PERMISSION_GRANTED) { 83 requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 84 REQ_PHOTOS_PERM); 85 } else { 86 PhotosContentJob.scheduleJob(MediaContentObserver.this); 87 updateButtons(); 88 } 89 } 90 }); 91 mCancelPhotosJob.setOnClickListener(new View.OnClickListener() { 92 @Override 93 public void onClick(View v) { 94 PhotosContentJob.cancelJob(MediaContentObserver.this); 95 updateButtons(); 96 } 97 }); 98 updateButtons(); 99 100 getContentResolver().registerContentObserver(MediaContentJob.MEDIA_URI, true, 101 mContentObserver); 102 } 103 104 @Override onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)105 public void onRequestPermissionsResult(int requestCode, String[] permissions, 106 int[] grantResults) { 107 if (requestCode == REQ_PHOTOS_PERM) { 108 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 109 PhotosContentJob.scheduleJob(MediaContentObserver.this); 110 updateButtons(); 111 } 112 } 113 } 114 updateButtons()115 void updateButtons() { 116 if (MediaContentJob.isScheduled(this)) { 117 mScheduleMediaJob.setEnabled(false); 118 mCancelMediaJob.setEnabled(true); 119 } else { 120 mScheduleMediaJob.setEnabled(true); 121 mCancelMediaJob.setEnabled(false); 122 } 123 if (PhotosContentJob.isScheduled(this)) { 124 mSchedulePhotosJob.setEnabled(false); 125 mCancelPhotosJob.setEnabled(true); 126 } else { 127 mSchedulePhotosJob.setEnabled(true); 128 mCancelPhotosJob.setEnabled(false); 129 } 130 } 131 132 @Override onDestroy()133 protected void onDestroy() { 134 super.onDestroy(); 135 getContentResolver().unregisterContentObserver(mContentObserver); 136 } 137 138 } 139