Search in sources :

Example 1 with UserSelectedArtwork

use of com.simplecity.amp_library.model.UserSelectedArtwork in project Shuttle by timusus.

the class MultiFetcher method loadData.

@Override
public InputStream loadData(Priority priority) throws Exception {
    InputStream inputStream = null;
    //Custom/user selected artwork. Loads from a specific source.
    UserSelectedArtwork userSelectedArtwork = ShuttleApplication.getInstance().userSelectedArtwork.get(artworkProvider.getArtworkKey());
    if (userSelectedArtwork != null) {
        switch(userSelectedArtwork.type) {
            case ArtworkProvider.Type.MEDIA_STORE:
                dataFetcher = new MediaStoreFetcher(artworkProvider);
                break;
            case ArtworkProvider.Type.FOLDER:
                dataFetcher = new FolderFetcher(artworkProvider, new File(userSelectedArtwork.path));
                break;
            case ArtworkProvider.Type.TAG:
                dataFetcher = new TagFetcher(artworkProvider);
                break;
            case ArtworkProvider.Type.LAST_FM:
                dataFetcher = new LastFmFetcher(artworkProvider);
                break;
            case ArtworkProvider.Type.ITUNES:
                dataFetcher = new ItunesFetcher(artworkProvider);
                break;
        }
        inputStream = loadData(dataFetcher, priority);
    }
    //Check the MediaStore
    if (inputStream == null && !SettingsManager.getInstance().ignoreMediaStoreArtwork()) {
        dataFetcher = new MediaStoreFetcher(artworkProvider);
        inputStream = loadData(dataFetcher, priority);
    }
    if (inputStream == null) {
        if (SettingsManager.getInstance().preferEmbeddedArtwork()) {
            //Check tags
            if (!SettingsManager.getInstance().ignoreEmbeddedArtwork()) {
                dataFetcher = new TagFetcher(artworkProvider);
                inputStream = loadData(dataFetcher, priority);
            }
            //Check folders
            if (inputStream == null && !SettingsManager.getInstance().ignoreFolderArtwork()) {
                dataFetcher = new FolderFetcher(artworkProvider, null);
                inputStream = loadData(dataFetcher, priority);
            }
        } else {
            //Check folders
            if (!SettingsManager.getInstance().ignoreFolderArtwork()) {
                dataFetcher = new FolderFetcher(artworkProvider, null);
                inputStream = loadData(dataFetcher, priority);
            }
            //Check tags
            if (inputStream == null && !SettingsManager.getInstance().ignoreEmbeddedArtwork()) {
                dataFetcher = new TagFetcher(artworkProvider);
                inputStream = loadData(dataFetcher, priority);
            }
        }
    }
    if (inputStream == null) {
        if (allowOfflineDownload || (SettingsManager.getInstance().canDownloadArtworkAutomatically() && ShuttleUtils.isOnline(true))) {
            //Last FM
            if (SettingsManager.getInstance().preferLastFM()) {
                dataFetcher = new LastFmFetcher(artworkProvider);
                inputStream = loadData(dataFetcher, priority);
            } else {
                dataFetcher = new ItunesFetcher(artworkProvider);
                inputStream = loadData(dataFetcher, priority);
            }
        }
    }
    return inputStream;
}
Also used : UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) InputStream(java.io.InputStream) File(java.io.File)

Example 2 with UserSelectedArtwork

use of com.simplecity.amp_library.model.UserSelectedArtwork in project Shuttle by timusus.

the class ShuttleApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    refWatcher = LeakCanary.install(this);
    sInstance = this;
    if (BuildConfig.DEBUG) {
        Log.w(TAG, "**Debug mode is ON**");
    //            enableStrictMode();
    }
    //Crashlytics
    CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
    Fabric.with(this, new Crashlytics.Builder().core(core).answers(new Answers()).build(), new Crashlytics());
    //Firebase Analytics
    FirebaseAnalytics.getInstance(this);
    //https://github.com/square/leakcanary/blob/master/leakcanary-android/src/main/java/com/squareup/leakcanary/AndroidExcludedRefs.java
    try {
        Class<?> cls = Class.forName("android.sec.clipboard.ClipboardUIManager");
        Method m = cls.getDeclaredMethod("getInstance", Context.class);
        Object o = m.invoke(null, this);
    } catch (Exception ignored) {
    }
    VideoCastManager.initialize(this, new CastConfiguration.Builder(Config.CHROMECAST_APP_ID).enableLockScreen().enableNotification().build());
    final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    setIsUpgraded(mPrefs.getBoolean("pref_theme_gold", false));
    // files with readAgain flag set to false, so always check KEY_HAS_SET_DEFAULT_VALUES
    if (!mPrefs.getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, false)) {
        PreferenceManager.setDefaultValues(this, R.xml.settings_artwork, true);
        PreferenceManager.setDefaultValues(this, R.xml.settings_blacklist, true);
        PreferenceManager.setDefaultValues(this, R.xml.settings_display, true);
        PreferenceManager.setDefaultValues(this, R.xml.settings_headers, true);
        PreferenceManager.setDefaultValues(this, R.xml.settings_headset, true);
        PreferenceManager.setDefaultValues(this, R.xml.settings_scrobbling, true);
        PreferenceManager.setDefaultValues(this, R.xml.settings_themes, true);
    }
    // Turn off logging for jaudiotagger.
    jaudioTaggerLogger1.setLevel(Level.OFF);
    jaudioTaggerLogger2.setLevel(Level.OFF);
    TagOptionSingleton.getInstance().setPadNumbers(true);
    SettingsManager.getInstance().incrementLaunchCount();
    startService(new Intent(this, EqualizerService.class));
    Observable.fromCallable(() -> {
        Query query = new Query.Builder().uri(CustomArtworkTable.URI).projection(new String[] { CustomArtworkTable.COLUMN_ID, CustomArtworkTable.COLUMN_KEY, CustomArtworkTable.COLUMN_TYPE, CustomArtworkTable.COLUMN_PATH }).build();
        SqlUtils.createActionableQuery(ShuttleApplication.this, cursor -> userSelectedArtwork.put(cursor.getString(cursor.getColumnIndexOrThrow(CustomArtworkTable.COLUMN_KEY)), new UserSelectedArtwork(cursor.getInt(cursor.getColumnIndexOrThrow(CustomArtworkTable.COLUMN_TYPE)), cursor.getString(cursor.getColumnIndexOrThrow(CustomArtworkTable.COLUMN_PATH)))), query);
        return null;
    }).subscribeOn(Schedulers.io()).subscribe();
    //Clean up the 'most played' playlist. We delay this call to allow the app to finish launching,
    //since it's not time critical.
    new Handler().postDelayed(this::cleanMostPlayedPlaylist, 5000);
    //Clean up the genres database - remove any genres which contain no songs.
    new Handler().postDelayed(this::cleanGenres, 7500);
    //Clean up any old, unused resources.
    new Handler().postDelayed(this::deleteOldResources, 10000);
}
Also used : CrashlyticsCore(com.crashlytics.android.core.CrashlyticsCore) Answers(com.crashlytics.android.answers.Answers) Query(com.simplecity.amp_library.model.Query) SharedPreferences(android.content.SharedPreferences) Handler(android.os.Handler) Intent(android.content.Intent) Method(java.lang.reflect.Method) EqualizerService(com.simplecity.amp_library.services.EqualizerService) Crashlytics(com.crashlytics.android.Crashlytics) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork)

Example 3 with UserSelectedArtwork

use of com.simplecity.amp_library.model.UserSelectedArtwork in project Shuttle by timusus.

the class ArtworkDialog method showDialog.

public static void showDialog(Context context, ArtworkProvider artworkProvider) {
    View customView = LayoutInflater.from(context).inflate(R.layout.dialog_artwork, null);
    ArtworkAdapter artworkAdapter = new ArtworkAdapter();
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
    RecyclerView recyclerView = (RecyclerView) customView.findViewById(R.id.recyclerView);
    recyclerView.addItemDecoration(new SpacesItemDecoration(16));
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemViewCacheSize(0);
    recyclerView.setRecyclerListener(holder -> {
        if (holder.getAdapterPosition() != -1) {
            artworkAdapter.items.get(holder.getAdapterPosition()).recycle(holder);
        }
    });
    artworkAdapter.items.add(0, new LoadingView());
    artworkAdapter.notifyDataSetChanged();
    recyclerView.setAdapter(artworkAdapter);
    ArtworkView.GlideListener glideListener = artworkView -> {
        int index = artworkAdapter.items.indexOf(artworkView);
        if (index != -1) {
            artworkAdapter.removeItem(index);
        }
    };
    List<AdaptableItem> adaptableItems = new ArrayList<>();
    UserSelectedArtwork userSelectedArtwork = ShuttleApplication.getInstance().userSelectedArtwork.get(artworkProvider.getArtworkKey());
    if (userSelectedArtwork != null) {
        File file = null;
        if (userSelectedArtwork.path != null) {
            file = new File(userSelectedArtwork.path);
        }
        ArtworkView artworkView = new ArtworkView(userSelectedArtwork.type, artworkProvider, glideListener, file, true);
        artworkView.setSelected(true);
        adaptableItems.add(artworkView);
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.MEDIA_STORE) {
        adaptableItems.add(new ArtworkView(ArtworkProvider.Type.MEDIA_STORE, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.TAG) {
        adaptableItems.add(new ArtworkView(ArtworkProvider.Type.TAG, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.LAST_FM) {
        adaptableItems.add(new ArtworkView(ArtworkProvider.Type.LAST_FM, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.ITUNES) {
        adaptableItems.add(new ArtworkView(ArtworkProvider.Type.ITUNES, artworkProvider, glideListener));
    }
    //Dummy Folder ArtworkView - will be replaced or removed depending on availability of folder images
    ArtworkView folderView = new ArtworkView(ArtworkProvider.Type.FOLDER, null, null);
    adaptableItems.add(folderView);
    artworkAdapter.setItems(adaptableItems);
    Observable.fromCallable(artworkProvider::getFolderArtworkFiles).subscribeOn(Schedulers.io()).subscribeOn(AndroidSchedulers.mainThread()).subscribe(files -> {
        artworkAdapter.removeItem(artworkAdapter.items.indexOf(folderView));
        if (files != null) {
            Stream.of(files).filter(file -> userSelectedArtwork == null || !file.getPath().equals(userSelectedArtwork.path)).forEach(file -> {
                artworkAdapter.addItem(new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, false));
            });
        }
    }, error -> {
        Log.e(TAG, "Error getting folder artwork files.. " + error.toString());
    });
    DialogUtils.getBuilder(context).title(context.getString(R.string.artwork_edit)).customView(customView, false).autoDismiss(false).positiveText(context.getString(R.string.save)).onPositive((dialog, which) -> {
        if (artworkAdapter.checkedItem != null) {
            ContentValues values = new ContentValues();
            values.put(CustomArtworkTable.COLUMN_KEY, artworkProvider.getArtworkKey());
            values.put(CustomArtworkTable.COLUMN_TYPE, artworkAdapter.checkedItem.type);
            values.put(CustomArtworkTable.COLUMN_PATH, artworkAdapter.checkedItem.file == null ? null : artworkAdapter.checkedItem.file.getPath());
            context.getContentResolver().insert(CustomArtworkTable.URI, values);
            ShuttleApplication.getInstance().userSelectedArtwork.put(artworkProvider.getArtworkKey(), new UserSelectedArtwork(artworkAdapter.checkedItem.type, artworkAdapter.checkedItem.file == null ? null : artworkAdapter.checkedItem.file.getPath()));
        } else {
            context.getContentResolver().delete(CustomArtworkTable.URI, CustomArtworkTable.COLUMN_KEY + "='" + artworkProvider.getArtworkKey().replaceAll("'", "\''") + "'", null);
            ShuttleApplication.getInstance().userSelectedArtwork.remove(artworkProvider.getArtworkKey());
        }
        dialog.dismiss();
    }).negativeText(context.getString(R.string.close)).onNegative((dialog, which) -> dialog.dismiss()).neutralText(context.getString(R.string.artwork_gallery)).onNeutral((dialog, which) -> RxImagePicker.with(context).requestImage(Sources.GALLERY).flatMap(uri -> {
        File dir = new File(ShuttleApplication.getInstance().getFilesDir() + "/shuttle/custom_artwork/" + artworkProvider.getArtworkKey().hashCode() + "/");
        if (!dir.exists()) {
            dir.mkdirs();
        } else {
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (String child : children) {
                    new File(dir, child).delete();
                }
            }
        }
        File file = new File(dir.getPath() + System.currentTimeMillis() + ".artwork");
        try {
            file.createNewFile();
            if (file.exists()) {
                return RxImageConverters.uriToFile(context, uri, file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }).filter(file -> file != null && file.exists()).subscribe(file -> {
        if (artworkAdapter.getItemCount() != 0) {
            File aFile = ((ArtworkView) artworkAdapter.items.get(0)).file;
            if (aFile != null && aFile.getPath().contains(artworkProvider.getArtworkKey())) {
                artworkAdapter.removeItem(0);
            }
        }
        ArtworkView artworkView = new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, true);
        artworkAdapter.addItem(0, artworkView);
        artworkAdapter.selectItem(0);
        recyclerView.scrollToPosition(0);
    })).cancelable(false).show();
}
Also used : R(com.simplecity.amp_library.R) Context(android.content.Context) SpacesItemDecoration(com.simplecity.amp_library.ui.recyclerview.SpacesItemDecoration) Stream(com.annimon.stream.Stream) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) ItemAdapter(com.simplecity.amp_library.ui.adapters.ItemAdapter) ArrayList(java.util.ArrayList) Observable(rx.Observable) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) LoadingView(com.simplecity.amp_library.ui.modelviews.LoadingView) Schedulers(rx.schedulers.Schedulers) View(android.view.View) RxImageConverters(com.mlsdev.rximagepicker.RxImageConverters) Log(android.util.Log) RxImagePicker(com.mlsdev.rximagepicker.RxImagePicker) ArtworkModel(com.simplecity.amp_library.model.ArtworkModel) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) LayoutInflater(android.view.LayoutInflater) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) IOException(java.io.IOException) ArtworkProvider(com.simplecity.amp_library.model.ArtworkProvider) CustomArtworkTable(com.simplecity.amp_library.sql.databases.CustomArtworkTable) File(java.io.File) RecyclerView(android.support.v7.widget.RecyclerView) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) ContentValues(android.content.ContentValues) AdaptableItem(com.simplecity.amp_library.model.AdaptableItem) Sources(com.mlsdev.rximagepicker.Sources) ContentValues(android.content.ContentValues) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) ArrayList(java.util.ArrayList) AdaptableItem(com.simplecity.amp_library.model.AdaptableItem) IOException(java.io.IOException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) LoadingView(com.simplecity.amp_library.ui.modelviews.LoadingView) View(android.view.View) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) RecyclerView(android.support.v7.widget.RecyclerView) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) LoadingView(com.simplecity.amp_library.ui.modelviews.LoadingView) RecyclerView(android.support.v7.widget.RecyclerView) SpacesItemDecoration(com.simplecity.amp_library.ui.recyclerview.SpacesItemDecoration) File(java.io.File)

Aggregations

UserSelectedArtwork (com.simplecity.amp_library.model.UserSelectedArtwork)3 File (java.io.File)2 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Handler (android.os.Handler)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 Stream (com.annimon.stream.Stream)1 Crashlytics (com.crashlytics.android.Crashlytics)1 Answers (com.crashlytics.android.answers.Answers)1 CrashlyticsCore (com.crashlytics.android.core.CrashlyticsCore)1 RxImageConverters (com.mlsdev.rximagepicker.RxImageConverters)1 RxImagePicker (com.mlsdev.rximagepicker.RxImagePicker)1 Sources (com.mlsdev.rximagepicker.Sources)1 R (com.simplecity.amp_library.R)1