Added receivers
parent
2543eb1132
commit
e4720c077c
|
@ -13,13 +13,23 @@
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme">
|
||||||
<service
|
<service
|
||||||
android:name=".service.LibraryService"
|
android:name=".service.LibraryService"
|
||||||
android:enabled="true" />
|
android:enabled="true"
|
||||||
|
android:launchMode="singleInstance"
|
||||||
|
/>
|
||||||
|
<!--android:process=":sync_service"-->
|
||||||
|
<receiver android:name=".service.PowerConnectionReceiver">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
<!--<receiver android:name=".service.PowerConnectionReceiver">-->
|
<receiver
|
||||||
<!--<intent-filter>-->
|
android:name=".tracker.ForceClearReceiver"
|
||||||
<!--<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />-->
|
android:exported="false">
|
||||||
<!--</intent-filter>-->
|
<intent-filter>
|
||||||
<!--</receiver>-->
|
<action android:name="max.music_cyclon.force_clear" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".SynchronizeActivity"
|
android:name=".SynchronizeActivity"
|
||||||
|
@ -31,7 +41,7 @@
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity android:name=".preference.MainPreferenceActivity"/>
|
<activity android:name=".preference.MainPreferenceActivity" />
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
|
@ -2,6 +2,7 @@ package max.music_cyclon.service;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.IntentService;
|
import android.app.IntentService;
|
||||||
|
import android.app.PendingIntent;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
@ -13,17 +14,15 @@ import android.os.Messenger;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.maxmpz.poweramp.player.PowerampAPI;
|
import com.maxmpz.poweramp.player.PowerampAPI;
|
||||||
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
@ -56,6 +55,8 @@ public class LibraryService extends IntentService {
|
||||||
public static final int MSG_STARTED = 4;
|
public static final int MSG_STARTED = 4;
|
||||||
public static final int MSG_FINISHED = 5;
|
public static final int MSG_FINISHED = 5;
|
||||||
|
|
||||||
|
public static final String ARGUMENT_CONFIGS = "configs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps track of all current registered clients.
|
* Keeps track of all current registered clients.
|
||||||
*/
|
*/
|
||||||
|
@ -75,7 +76,7 @@ public class LibraryService extends IntentService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onHandleIntent(Intent intent) {
|
protected void onHandleIntent(Intent intent) {
|
||||||
Parcelable[] configs = intent.getParcelableArrayExtra("configs");
|
Parcelable[] configs = intent.getParcelableArrayExtra(ARGUMENT_CONFIGS);
|
||||||
|
|
||||||
ProgressUpdater updater = new ProgressUpdater(this);
|
ProgressUpdater updater = new ProgressUpdater(this);
|
||||||
|
|
||||||
|
@ -83,7 +84,7 @@ public class LibraryService extends IntentService {
|
||||||
|
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
!= PackageManager.PERMISSION_GRANTED) {
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
updater.showMessage("No permission to write!", false);
|
updater.showMessage("No permission to write!");
|
||||||
finished();
|
finished();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -98,7 +99,7 @@ public class LibraryService extends IntentService {
|
||||||
BeetsFetcher fetcher = new BeetsFetcher(address, getResources());
|
BeetsFetcher fetcher = new BeetsFetcher(address, getResources());
|
||||||
|
|
||||||
if (root.exists() && !root.isDirectory()) {
|
if (root.exists() && !root.isDirectory()) {
|
||||||
updater.showMessage("Library is no dictionary! Fix manually", false);
|
updater.showMessage("Library is no dictionary! Fix manually");
|
||||||
finished();
|
finished();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -108,14 +109,22 @@ public class LibraryService extends IntentService {
|
||||||
FileTracker tracker = new FileTracker(getApplicationContext());
|
FileTracker tracker = new FileTracker(getApplicationContext());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
updater.showMessage("Cleaning library", true);
|
updater.showOngoingMessage("Cleaning library");
|
||||||
tracker.delete();
|
tracker.delete();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.exists() && root.list().length != 0) {
|
if (root.exists() && root.list().length != 0) {
|
||||||
updater.showMessage("Library not empty! Clean in manually", false);
|
NotificationCompat.Builder builder = updater.notificationBuilder();
|
||||||
|
builder.setContentTitle("Library not empty! Clean in manually");
|
||||||
|
Intent libraryIntent = new Intent();
|
||||||
|
libraryIntent.setAction("max.music_cyclon.force_clear");
|
||||||
|
|
||||||
|
PendingIntent pending = PendingIntent.getBroadcast(this, 0, libraryIntent, 0);
|
||||||
|
builder.addAction(android.R.drawable.ic_delete, "Clear now", pending);
|
||||||
|
updater.updateNotification(builder);
|
||||||
|
|
||||||
finished();
|
finished();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -126,25 +135,21 @@ public class LibraryService extends IntentService {
|
||||||
SynchronizeConfig config = (SynchronizeConfig) parcelable;
|
SynchronizeConfig config = (SynchronizeConfig) parcelable;
|
||||||
List<Item> items;
|
List<Item> items;
|
||||||
try {
|
try {
|
||||||
updater.showMessage("Fetching music information for %s", true, config.getName());
|
updater.showOngoingMessage("Fetching music information for %s", config.getName());
|
||||||
items = fetcher.fetch(config);
|
items = fetcher.fetch(config);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.wtf("WTF", e);
|
Log.wtf("WTF", e);
|
||||||
updater.showMessage("Remote not available", false);
|
updater.showMessage("Remote not available");
|
||||||
finished();
|
finished();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
updater.showMessage("Mixing new music for %s!", true, config.getName());
|
updater.showOngoingMessage("Mixing new music for %s!", config.getName());
|
||||||
updater.setMaximumProgress(items.size());
|
updater.setMaximumProgress(items.size());
|
||||||
|
|
||||||
for (Item item : items) {
|
for (Item item : items) {
|
||||||
try {
|
String url = address + "/item/" + item.getID() + "/file";
|
||||||
URI uri = new URI(address + "/item/" + item.getId() + "/file");
|
tasks.add(new DownloadTask(config, url, item.getPath(), tracker, updater));
|
||||||
tasks.add(new DownloadTask(config, uri, item.getPath(), tracker, updater));
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,11 +168,7 @@ public class LibraryService extends IntentService {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
updater.showMessage("Musik aktualisiert", false);
|
updater.showMessage("Musik aktualisiert");
|
||||||
|
|
||||||
// Update last_updated info
|
|
||||||
SharedPreferences preferences = getSharedPreferences("info", MODE_PRIVATE);
|
|
||||||
preferences.edit().putLong("last_updated", System.currentTimeMillis()).apply();
|
|
||||||
|
|
||||||
// Poweramp support
|
// Poweramp support
|
||||||
Intent poweramp = new Intent(PowerampAPI.Scanner.ACTION_SCAN_DIRS);
|
Intent poweramp = new Intent(PowerampAPI.Scanner.ACTION_SCAN_DIRS);
|
||||||
|
@ -175,6 +176,7 @@ public class LibraryService extends IntentService {
|
||||||
poweramp.putExtra(PowerampAPI.Scanner.EXTRA_FULL_RESCAN, true);
|
poweramp.putExtra(PowerampAPI.Scanner.EXTRA_FULL_RESCAN, true);
|
||||||
startService(poweramp);
|
startService(poweramp);
|
||||||
|
|
||||||
|
|
||||||
finished();
|
finished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package max.music_cyclon.tracker;
|
||||||
|
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Environment;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import max.music_cyclon.service.ProgressUpdater;
|
||||||
|
|
||||||
|
|
||||||
|
public class ForceClearReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
File root = new File(Environment.getExternalStorageDirectory(), "library");
|
||||||
|
try {
|
||||||
|
FileUtils.deleteDirectory(root);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e("LIBRARY", "Failed to delete library", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
manager.cancel(ProgressUpdater.NOTIFICATION_ID);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue