I am building simple android app responsible for image encryption. This application should automatically detect images with sensitive data (documents or faces) right after it is stored on device and suggest to user to encrypt it using notification with message like "Hey dude! We found unencrypted sensitive images on your storage. Would you mind to encrypt it?"
What I need to do is to detect if some new file appeared in DCIM folder, analyze it if it contains sensitive data and if it does then push such notification so user can click on it and encrypt that image. I need this app will work with latest android versions (From android O I can't listen to implicit broadcasts and run service on background) so I used FirebaseJobDispatcher where I set contentUriTrigger for DCIM folder as a trigger.
The problem is that this trigger does not work (my JobService has not been launched) when I take a picture and I can't find out why. There is really poor documentation about it. Everything I know I have found in source code from official repo: https://github.com/firebase/firebase-jobdispatcher-android
Here is my code:
MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_safe)
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
val dispatcher = FirebaseJobDispatcher(GooglePlayDriver(this))
val dcimUri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path)
val myJob = dispatcher.newJobBuilder()
.setService(NotificationJob::class.java)
.setTag("com.something.something.NOTIFICATION_JOB")
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.contentUriTrigger(listOf(
ObservedUri(dcimUri, ObservedUri.Flags.FLAG_NOTIFY_FOR_DESCENDANTS)
)))
.build()
dispatcher.mustSchedule(myJob)
}}
JobService:
class NotificationJob: JobService() {
override fun onStopJob(job: JobParameters?): Boolean {
Log.d("NotJob", "job stopped")
return true
}
override fun onStartJob(job: JobParameters?): Boolean {
Notifier.pushEncryptonNotification(applicationContext)
Log.d("NotJob", "job started")
return true
}}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.saomething.something">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<service
android:exported="false"
android:name=".job.NotificationJob">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
</application>
INFO:
- For those who don't know WRITE permission include also READ.
- Notifier is my manager class which handles android notifications and you can assume that it works.
- Logic for image analysis is not yet implemented. I want to add it into onStartJob method. It will run asynchronously and only if sensitive data detected it will call the Notifier (for now I just want to see notification every time DCIM folder will change)
Did anybody have such problem before? If not, is there any other way to implement such behavior? Please help!