Skip to content

Instantly share code, notes, and snippets.

@barron9
Created January 26, 2019 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barron9/71fee722348c34dc5a027528bfe947d6 to your computer and use it in GitHub Desktop.
Save barron9/71fee722348c34dc5a027528bfe947d6 to your computer and use it in GitHub Desktop.
mediatrackerservice.java
package com.generalmobile.app.gmfilemanager.core.services;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.ContextCompat;
import com.generalmobile.app.gmfilemanager.GmFileManagerApplication;
import com.generalmobile.app.gmfilemanager.R;
import com.generalmobile.app.gmfilemanager.db.DaoSession;
import com.generalmobile.app.gmfilemanager.db.MediaFile;
import com.generalmobile.app.gmfilemanager.explorer.ExplorerActivity;
import com.generalmobile.app.gmfilemanager.explorer.ExplorerPresenter;
import com.generalmobile.app.gmfilemanager.utils.ColorUtils;
import com.generalmobile.library.common.utils.SdkUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import javax.inject.Inject;
public class MediaTrackerService extends Service {
public final int MEDIA_NOTIFICATION = 1903;
@Inject
DaoSession daoSession;
String channelId = "mediaBg";
private android.support.v4.app.NotificationCompat.Builder notification;
private NotificationManagerCompat mNotificationManager;
private File file;
private SharedPreferences preferences;
private ArrayList<Uri> uriList = new ArrayList<>();
private ArrayList<String> pathList = new ArrayList<>();
private int count = 0;
private MyCount counter;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mNotificationManager = NotificationManagerCompat.from(this);
preferences = PreferenceManager.getDefaultSharedPreferences(GmFileManagerApplication.getContext());
((GmFileManagerApplication) GmFileManagerApplication.getContext()).getApplicationComponent().inject(this);
counter = new MyCount(5000, 1000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// images
getContentResolver().registerContentObserver(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true, new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Media media = readFromMediaStore(
getApplicationContext(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (media != null) {
file = new File(media.file.getPath());
Date fileData = new Date(file.lastModified());
if (System.currentTimeMillis() - fileData.getTime() > 3000) {
} else {
if (!uriList.contains(uri)) {
uriList.add(uri);
count++;
}
if (!media.file.getPath().contains("WhatsApp") && !media.file.getPath().contains("Camera")) {
MediaFile mediaFile = new MediaFile();
if (!pathList.contains(file.getPath())) {
pathList.add(file.getPath());
mediaFile.setPath(file.getPath());
daoSession.getMediaFileDao().insert(mediaFile);
if (counter != null)
counter.timerCreation();
}
if (counter != null) {
counter.start();
}
}
}
}
}
});
// music
getContentResolver().registerContentObserver(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
true, new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Media media = readFromMediaStore(
getApplicationContext(),
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
if (media != null) {
file = new File(media.file.getPath());
Date fileData = new Date(file.lastModified());
if (System.currentTimeMillis() - fileData.getTime() > 3000) {
} else {
if (!uriList.contains(uri)) {
uriList.add(uri);
count++;
}
boolean haveGmMusic = ExplorerPresenter.isAppInstalled(GmFileManagerApplication.getContext(), "com.generalmobile.app.musicplayer");
if (!media.file.getPath().contains("WhatsApp") && !haveGmMusic) {
MediaFile mediaFile = new MediaFile();
if (!pathList.contains(file.getPath())) {
pathList.add(file.getPath());
mediaFile.setPath(file.getPath());
daoSession.insert(mediaFile);
if (counter != null)
counter.timerCreation();
}
if (counter != null) {
counter.start();
}
}
}
}
}
});
// video
getContentResolver().registerContentObserver(
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
true, new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Media media = readFromMediaStore(
getApplicationContext(),
MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
if (media != null) {
file = new File(media.file.getPath());
Date fileData = new Date(file.lastModified());
if (System.currentTimeMillis() - fileData.getTime() > 3000) {
} else {
if (!uriList.contains(uri)) {
uriList.add(uri);
count++;
}
if (!media.file.getPath().contains("WhatsApp") && !media.file.getPath().contains("Camera")) {
MediaFile mediaFile = new MediaFile();
if (!pathList.contains(file.getPath())) {
pathList.add(file.getPath());
mediaFile.setPath(file.getPath());
daoSession.insert(mediaFile);
if (counter != null)
counter.timerCreation();
}
if (counter != null) {
counter.start();
}
}
}
}
}
});
return START_STICKY;
}
private Media readFromMediaStore(Context context, Uri uri) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Cursor cursor = context.getContentResolver().query(uri, null, null,
null, "date_added DESC");
Media media = null;
if (cursor != null && cursor.moveToNext()) {
int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
String filePath = cursor.getString(dataColumn);
int mimeTypeColumn = cursor
.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE);
String mimeType = cursor.getString(mimeTypeColumn);
media = new Media(new File(filePath), mimeType);
}
if (cursor != null) {
cursor.close();
}
return media;
} else
return null;
}
private android.support.v4.app.NotificationCompat.Builder createNotification() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence channelName = "MediaService";
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_MAX);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
android.support.v4.app.NotificationCompat.Builder notificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this, channelId);
Intent openIntent = new Intent(this, ExplorerActivity.class);
openIntent.putStringArrayListExtra("mediapath", pathList);
openIntent.putExtra("path", file.getParent());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContentText(getString(R.string.new_media_found))
.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.new_media_found)))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
if (SdkUtils.hasLollipop()) {
notificationBuilder = notificationBuilder
.setSmallIcon(R.drawable.status_dark_ico)
.setColor(ColorUtils.getColorFromAttr(R.attr.colorAccent));
} else {
notificationBuilder = notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
return notificationBuilder;
}
private class Media {
private File file;
private String type;
public Media(File file, String type) {
this.file = file;
this.type = type;
}
public String getType() {
return type;
}
public File getFile() {
return file;
}
}
public class MyCount extends CountDownTimer {
private MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
boolean showNotification = preferences.getBoolean("isShow", true);
if (count > 3 && showNotification) {
notification = createNotification();
if (notification != null) {
mNotificationManager.notify(MEDIA_NOTIFICATION, notification.build());
}
}
count = 0;
}
public void timerCreation() {
counter.cancel();
counter = new MyCount(5000, 1);
counter.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment