Back to project page LyricHere.
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.
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; } }