package com.yxkang.android.sample.service;

import android.app.IntentService;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p/>
 */
public class MediaModifyService extends IntentService {

    private final ArrayList<AudioInfo> audioInfos = new ArrayList<>();
    private static final String TAG = MediaModifyService.class.getSimpleName();

    private static final Logger logger = LoggerFactory.getLogger(MediaModifyService.class);

    public MediaModifyService() {
        super("MediaModifyService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            loadMediaInfo();
        }
    }

    private void loadMediaInfo() {
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String display_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                Log.i(TAG, display_name + "/" + title);
                if ((!display_name.contains(title) || (title.contains(";") | title.contains("[") | title.contains("]")))) {
                    String _title = parseTitle(display_name);
                    if (!_title.equals(title)) {
                        logger.warn("{}/{}", display_name, title);
                        title = _title;
                        AudioInfo info = new AudioInfo();
                        info.data = data;
                        info.title = title;
                        info.display_name = display_name;
                        audioInfos.add(info);
                    }
                }
            }
            cursor.close();
        }
        if (audioInfos.size() > 0) {
            modifyMediaInfo();
        } else {
            logger.info("no need to modify media information");
        }
    }

    private String parseTitle(String displayName) {
        int end = displayName.lastIndexOf("[");
        if (end == -1) {
            end = displayName.lastIndexOf(".");
        }
        return displayName.substring(0, end).trim();
    }

    private void modifyMediaInfo() {
        ContentResolver resolver = getContentResolver();
        for (AudioInfo info : audioInfos) {
            logger.info("{}/{}", info.display_name, info.title);
            String where = MediaStore.Audio.Media.DATA + "=? and " + MediaStore.Audio.Media.DISPLAY_NAME + "=?";
            String[] selectionArgs = new String[]{info.data, info.display_name};
            ContentValues values = new ContentValues();
            values.put(MediaStore.Audio.Media.TITLE, info.title);
            int updated = resolver.update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values, where, selectionArgs);
            logger.debug("updated = {}", updated);
        }
        audioInfos.clear();
    }

    private static class AudioInfo {
        String data;
        String display_name;
        String title;
    }
}

@@ -212,16 +212,41 @@
 Cursor;%0A
+import android.os.Build;%0A
 import a
@@ -273,16 +273,16 @@
 aStore;%0A
-
 import a
@@ -1700,16 +1700,36 @@
 g.i(TAG,
+ %22loadMediaInfo: %22 +
  display
@@ -1981,24 +1981,105 @@
 s(title)) %7B%0A
+                        if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.M) %7B%0A    
             
@@ -2101,12 +2101,13 @@
 ger.
-warn
+debug
 (%22%7B%7D
@@ -2126,32 +2126,179 @@
 y_name, title);%0A
+                        %7D else %7B%0A                            Log.d(TAG, %22loadMediaInfo: %22 + display_name + %22/%22 + title);%0A                        %7D%0A
                 
@@ -2762,59 +2762,230 @@
     
-logger.info(%22no need to modify media information%22);
+if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.M) %7B%0A                logger.info(%22no need to modify media information%22);%0A            %7D else %7B%0A                Log.v(TAG, %22no need to modify media information%22);%0A            %7D
 %0A   
@@ -3395,60 +3395,252 @@
     
-logger.info(%22%7B%7D/%7B%7D%22, info.display_name, info.title);
+if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.M) %7B%0A                logger.info(%22%7B%7D/%7B%7D%22, info.display_name, info.title);%0A            %7D else %7B%0A                Log.i(TAG, %22modifyMediaInfo: %22 + info.display_name + %22/%22 + info.title);%0A            %7D
 %0A   
@@ -4086,46 +4086,219 @@
     
-logger.debug(%22updated = %7B%7D%22, updated);
+if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.M) %7B%0A                logger.debug(%22updated = %7B%7D%22, updated);%0A            %7D else %7B%0A                Log.d(TAG, %22modifyMediaInfo: updated = %22 + updated);%0A            %7D
 %0A   

change log for android m