Newer
Older
VipVideo / app / src / main / java / me / bello / viptv / util / StringTool.java
bello on 21 Jan 2021 3 KB 下载功能
package me.bello.viptv.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @Info
 * @Author Bello
 * @Time 21-1-18 下午4:08
 * @Ver
 */
public class StringTool {

    /**
     * 处理webView的标题
     * @param title
     * @return
     */
    public static String getPageTitle(String title) {
        if (null == title || title.isEmpty() || title.toLowerCase().equals("null")) {
            return "";
        }

        if (title.contains("_")) {
            return title.split("_")[0];
        }
        if (title.contains(" ")) {
            return title.split(" ")[0];
        }
        if (title.contains("-")) {
            return title.split("-")[0];
        }
        return "";
    }

    /**
     * 判断string是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        if (null == str || str.isEmpty() || str.toLowerCase().equals("null")){
            return true;
        }
        return false;
    }


    public static void saveDownInfo(Context context, String title, String url, String downPercent, int notifyId) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("cache", 0);
        String down = sharedPreferences.getString("down", "");
        if (down != null && !down.isEmpty()) {
            JSONArray array = JSONArray.parseArray(down);
            boolean exist = false;
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.getJSONObject(i);
                if (obj.getString("title").equals(title) && obj.getString("url").equals(url)) {
                    obj.put("percent", downPercent);
                    exist = true;
                }
            }
            if (!exist){
                JSONObject obj = new JSONObject();
                obj.put("title", title);
                obj.put("url", url);
                obj.put("percent", downPercent);
                obj.put("notifyId", notifyId);
                array.add(obj);
            }
            sharedPreferences.edit().putString("down", JSONArray.toJSONString(array)).commit();

        } else {
            JSONObject obj = new JSONObject();
            obj.put("title", title);
            obj.put("url", url);
            obj.put("percent", downPercent);
            obj.put("notifyId", notifyId);
            JSONArray array = new JSONArray();
            array.add(obj);
            sharedPreferences.edit().putString("down", JSONArray.toJSONString(array)).commit();
        }

    }


    public static String getDownInfo(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("cache", 0);
       return sharedPreferences.getString("down", "");
    }


    public static void deleteDownInfo(Context context, String title){
        SharedPreferences sharedPreferences = context.getSharedPreferences("cache", 0);
        String down = sharedPreferences.getString("down", "");
        if (down != null && !down.isEmpty()) {
            JSONArray array = JSONArray.parseArray(down);
            int flag = -1;
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.getJSONObject(i);
                if (obj.getString("title").equals(title)) {
                   flag = i;
                }
            }
            if (flag != -1){
                array.remove(flag);
            }

            sharedPreferences.edit().putString("down", JSONArray.toJSONString(array)).commit();

        }
    }

}