Android Open Source - LyricHere Finder






From Project

Back to project page LyricHere.

License

The source code is released under:

Apache License

If you think the Android project LyricHere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cn.zhaiyifan.lyrichere.workers;
/*from   w  w w .  j  a v a  2  s  .c  o  m*/
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.Map;

import cn.zhaiyifan.lyrichere.Constants;
import cn.zhaiyifan.lyrichere.R;
import cn.zhaiyifan.lyrichere.model.Lyric;
import cn.zhaiyifan.lyrichere.utils.DbUtils;
import cn.zhaiyifan.lyrichere.utils.LyricUtils;
import cn.zhaiyifan.lyrichere.utils.Util;

/**
 * Created by yifan on 6/3/14.
 */
public class  Finder extends AsyncTask<File, Integer, Integer> {

    private static final String TAG = Finder.class.getSimpleName();
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
    private Context mContext;
    private File currentDir;

    public Finder(Context context) {
        this.mContext = context;
    }

    @Override
    protected void onPostExecute(Integer result) {
        Util.log(TAG, String.format("Find %d lyrics under %s", result, currentDir.getAbsolutePath()));
        mContext = null;
    }

    @Override
    protected Integer doInBackground(File... params) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        Integer result = 0;
        currentDir = params[0];

        boolean isRememberEncoding = sharedPreferences.getBoolean(mContext.getString(R.string.pref_key_remember_encoding), true);
        Map<String, String> pathEncodingMap = null;
        if (isRememberEncoding) {
            pathEncodingMap = DbUtils.getNonDefaultEncodingMap(mContext.getContentResolver());
        }

        Util.log(TAG, "Finder directory: " + currentDir.getAbsolutePath());

        String[] children = currentDir.list();

        if (children == null) {
            Util.log(TAG, "null children");
            return result;
        }

        boolean showHidden = false;
        boolean showSystem = false;

        LinkedList<String> fileList = new LinkedList<String>();

        for (String fileName : children) {
            fileList.add(currentDir.getAbsolutePath() + File.separator + fileName);
        }

        mContext.getContentResolver().delete(Constants.CONTENT_URI, null, null);

        while (!fileList.isEmpty()) {
            String currentFilePath = fileList.pop();

            File f = new File(currentFilePath);

            if (!f.exists()) {
                continue;
            }
            if (Util.isProtected(f) && !showSystem) {
                continue;
            }
            if (f.isHidden() && !showHidden) {
                continue;
            }

            // Add files under directory
            if (f.isDirectory()) {
                String[] newFileList = f.list();
                for (String newFileName : newFileList) {
                    fileList.add(currentFilePath + File.separator + newFileName);
                }
                continue;
            }

            // Not directory but not lyric file, ignore
            if (!currentFilePath.endsWith(".lrc")) {
                continue;
            }

            // If encoding of the same file has been changed, keep its original encoding.
            String encoding = null;
            if (isRememberEncoding && pathEncodingMap != null) {
                encoding = pathEncodingMap.get(f.getAbsolutePath());
            }
            // Not in map or remember old encoding disabled
            if (encoding == null) {
                encoding = sharedPreferences.getString(mContext.getString(R.string.pref_key_default_encoding), null);
            }

            if (encoding == null) {
                encoding = sharedPreferences.getString("pref_default_encoding_key", Constants.ENCODE_UTF_8);
            }

            Lyric lyric = LyricUtils.parseLyric(f, encoding);
            ContentValues values = DbUtils.getLyricContentValue(lyric,
                    f.getAbsolutePath(), System.currentTimeMillis(), encoding, mContext.getString(R.string.tag_not_found));

            // URI is registered at application AndroidManifest.xml
            Uri uri = mContext.getContentResolver().insert(Constants.CONTENT_URI, values);
            if (uri != null) {
                Util.log(TAG, String.format("(id %d): %s - %s, [%s]", Integer.valueOf(uri.getLastPathSegment()),
                        lyric.getArtist(), lyric.getTitle(), f.getAbsolutePath()));
            }
            result++;
        }
        return result;
    }
}




Java Source Code List

cn.zhaiyifan.lyrichere.ApplicationTest.java
cn.zhaiyifan.lyrichere.Constants.java
cn.zhaiyifan.lyrichere.MusicBroadcastReceiver.java
cn.zhaiyifan.lyrichere.adapters.LyricCursorAdapter.java
cn.zhaiyifan.lyrichere.db.DbHelper.java
cn.zhaiyifan.lyrichere.db.LyricContentProvider.java
cn.zhaiyifan.lyrichere.model.Lyric.java
cn.zhaiyifan.lyrichere.prefs.SettingsActivity.java
cn.zhaiyifan.lyrichere.prefs.SettingsFragment.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.AlphaPatternDrawable.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerDialog.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerPanelView.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerPreference.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.ColorPickerView.java
cn.zhaiyifan.lyrichere.prefs.colorpicker.Test.java
cn.zhaiyifan.lyrichere.ui.AboutActivity.java
cn.zhaiyifan.lyrichere.ui.DownloadFragment.java
cn.zhaiyifan.lyrichere.ui.ListScrollTextView.java
cn.zhaiyifan.lyrichere.ui.LyricExplorerActivity.java
cn.zhaiyifan.lyrichere.ui.LyricExplorerFragment.java
cn.zhaiyifan.lyrichere.ui.LyricPlayerActivity.java
cn.zhaiyifan.lyrichere.ui.LyricPlayerFragment.java
cn.zhaiyifan.lyrichere.ui.LyricSearchView.java
cn.zhaiyifan.lyrichere.ui.LyricView.java
cn.zhaiyifan.lyrichere.utils.DbUtils.java
cn.zhaiyifan.lyrichere.utils.FileUtils.java
cn.zhaiyifan.lyrichere.utils.LyricCache.java
cn.zhaiyifan.lyrichere.utils.LyricProvider.java
cn.zhaiyifan.lyrichere.utils.LyricUtils.java
cn.zhaiyifan.lyrichere.utils.Util.java
cn.zhaiyifan.lyrichere.workers.Finder.java
cn.zhaiyifan.lyrichere.workers.LyricEncodingUpdater.java
cn.zhaiyifan.lyrichere.workers.LyricLastVisitUpdater.java
cn.zhaiyifan.lyrichere.workers.LyricOpener.java