package com.hc.webapp;

import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.hc.lib.http.GetRequest;
import com.hc.lib.http.GetRequestListener;
import com.hc.lib.video.MVideoPlayer;
import com.hc.lib.video.PlayerHelper;
import com.hc.request.FJYDConfig;
import com.hc.request.core.CallServer;
import com.hc.request.core.DefaultCallback;
import com.hc.webapp.model.VideoInfo;
import com.hc.webapp.video.VideoControlDelegate;
import com.hc.webapp.web.AndroidToJS;
import com.hc.webapp.web.BaseWebActivity;
import com.jewel.mplayer.content.VideoData;
import com.orhanobut.logger.Logger;
import com.yanzhenjie.nohttp.RequestMethod;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends BaseWebActivity implements AndroidToJS.PlayVideoListener {

        final String url = com.hc.model.BuildConfig.Domain + com.hc.model.BuildConfig.port + "/h5v2/index.html";
        //final String url = "http://112.35.32.145:8090/h5v2/test/testapk.html";  //活动测试地址
    //final String url = "http://112.35.32.145:8090/h5v2/subject/peiqi/index.html?returnUrl=http%3A//112.35.32.145%3A8090/h5v2/test/haochuan.html%3F";
    //final String url ="http://112.35.32.145:8090/h5v2/subject/zt20190222/index.html";
    //final String url = com.hc.model.BuildConfig.Domain + com.hc.model.BuildConfig.port + "/h5v2/template/haochuan.html";
    //final String url ="http://112.35.32.145:8090/h5v2/sxyd_djbl.html?epgServer=http://111.20.42.147:33200&sourceId=1363&token=tgS09L3h7qp3w9-StjtSpqfq5";

//    public static final String[] urls = {"http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4",
//            "https://hnh5pic.oss-cn-shenzhen.aliyuncs.com/20180910_test.ts"};

//    private HCVideoPlayer videoPlayer;
    private MVideoPlayer videoPlayer;
    private VideoControlDelegate controlDelegate;

    private int floatVideoX = -1, floatVideoY = -1;
    private int floatVideoWidth = -1, floatVideoHeight = -1;
    private boolean isFullScreenVideo = false;
    private boolean hadFloatVideo = false;

    private String KEY = "jumptype";
    private String value;

    private VideoInfo lastVideoInfo;

    private String TAG ="MainActivity";

    //add by 许林
    //float density = 0;
    //基准屏幕参数，主要是确定小视频窗位置和大小用
    float baseScreenWidth = 1280f;
    float baseScreenHeight = 720f;

    int screenWidth = 0;
    int screenHeight = 0;

   // String myUid = "123456";

    //private VideoInfo currentFloatInfo;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;
        Log.d("MainActivity", "; width:" + screenWidth + "; height:" + screenHeight);

        videoPlayer = new MVideoPlayer(MainActivity.this);
        videoPlayer.setActivity(this);
        // 是否需要显示流量提示
//        videoPlayer.setNeedShowWifiTip(false);

        controlDelegate = new VideoControlDelegate(this, videoPlayer);
        controlDelegate.subscribeData();
        controlDelegate.getExitPlayingEvent().observe(this, aBoolean -> {
            //判断视频是否播放完成,来判断是否要储存上次播放时间
            videoPlayer.storeLastPlayTime();

            if (hadFloatVideo) {
                videoPlayer.hideAllWidget();
                changeVideo2Float();
            } else {
                destroyVideo();
            }
        });
        controlDelegate.getPlayNextEvent().observe(this, playNext -> {
            Logger.d("请求下一个视频 %s", playNext);
            if(playNext != null && playNext) {
                AndroidToJS.evaluateJavascript(getWebView(), AndroidToJS.JS_EVENT_PLAY_NEXT);
            }
        });

        videoPlayer.setOnPlayerPausedListener(controlDelegate);

        //test
        /*String url = "http://111.20.46.48/88889111/16/20181212/271299210/271299210.ts/index.m3u8?fmt=ts2hls&servicetype=0&icpid=&accounttype=1&limitflux=-1&limitdur=-1&GuardEncType=2&accountinfo=sUFt%2FPMULYuyWVFSA%2FVDqa8s2VFuOZhJ7OISct9i8%2BvpyEPZ9frA7SumfGNqZJc7cXCRWXKvMQgH9xz%2BgR3f8aXIggwNWymSGSoopWIO9N1E8D2zuDbB4ZzZmT1FJf2Q2qychd%2FPFaL5OX0k0v9ZdQ%3D%3D%3A20181213142720%2C13474100670%2C111.20.40.66%2C20181213142720%2CHC_SXYD_Program_1385%2C95E267385D3A3105C33CECCFA24E5215%2C%2C1%2C0%2C-1%2C3%2C1%2C770%2Csmgpack0000000000000000000000901%2C2990525%2C1%2CEND";

        playFullVideo(url,"嘟嘟玩具","123456","123",0,false,false,0);*/
//        videoPlayer.setLooping(false);
    }

   /* private void initMediaPlayer(){
        videoPlayer = new MVideoPlayer(MainActivity.this);
        videoPlayer.setActivity(this);
        controlDelegate = new VideoControlDelegate(this, videoPlayer);
        controlDelegate.subscribeData();
        controlDelegate.getExitPlayingEvent().observe(this, aBoolean -> {
            //判断视频是否播放完成,来判断是否要储存上次播放时间
            videoPlayer.storeLastPlayTime();

            if (hadFloatVideo) {
                videoPlayer.hideAllWidget();
                changeVideo2Float();
            } else {
                destroyVideo();
            }
        });
        controlDelegate.getPlayNextEvent().observe(this, playNext -> {
            Logger.d("请求下一个视频 %s", playNext);
            if(playNext != null && playNext) {
                AndroidToJS.evaluateJavascript(getWebView(), AndroidToJS.JS_EVENT_PLAY_NEXT);
            }
        });

        videoPlayer.setOnPlayerPausedListener(controlDelegate);
    }*/


    @Override
    protected void destroyVideoView() {
        destroyVideo();
    }

    @Override
    protected void delayShowFloatView() {
        //Log.d(TAG,"delayShowFloatView");
        if(lastVideoInfo != null){
            playFloatVideo(lastVideoInfo.url,lastVideoInfo.x,lastVideoInfo.y,lastVideoInfo.width,lastVideoInfo.height,
                    lastVideoInfo.uid,lastVideoInfo.sourceId,lastVideoInfo.title,lastVideoInfo.needMute);
            hasOneFloatViewToShow = false;
        }
    }

    @Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);
        value = intent.getStringExtra(KEY);
    }



    @Override
    protected String getIndexURL() {
        if(TextUtils.isEmpty(value)) {
            return url;
        }
        return url + "?" + KEY + "=" + value;
    }

    @Override
    protected String getJsInvokeName() {
        return "AndroidToJS";
    }

    @Override
    public void setPlayVideoListener(AndroidToJS androidToJS) {
        super.setPlayVideoListener(androidToJS);
        androidToJS.setPlayVideoListener(this);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if(!isFullScreenVideo){
            getWebView().requestFocus();
        }
        if (isFullScreenVideo && PlayerHelper.shouldOverrideKeyEvent(keyCode)) {
            Logger.d("当前按下键值：%s", keyCode);
            return controlDelegate.onKeyDown(keyCode, event);
        }

        return super.dispatchKeyEvent(event);
    }




    @Override
    protected void onStop() {
        super.onStop();
        boolean isForeground = BaseApp.isRunningForeground(this);
        if (!isForeground) {
            BaseApp.exit();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroyVideo();
    }


    @Override
    public void playFloatVideo(final String url, final int x, final int y, final int width, final int height,
                               final String uid, final String sourceId, final String title, boolean needMute) {
        //currentFloatInfo = new VideoInfo(url,x,y,width,height,uid,sourceId,title,needMute);
        if(!hasShowWebView){
            hasOneFloatViewToShow = true;
            lastVideoInfo = new VideoInfo(url,x,y,width,height,uid,sourceId,title,needMute);
            return;
        }
        isFullScreenVideo = false;
        this.hadFloatVideo = true;
        controlDelegate.setFullVideo(false);
        Logger.d("调用小窗口播放。地址(%s)，坐标(%s, %s)，宽高(%s, %s)", url, x, y, width, height);
        runOnUiThread(() -> playVideo(url, title, uid, sourceId, x, y, width, height, needMute));
    }

    @Override
    public void playFullVideo(final String url, final String title, final String uid, final String sourceId, final long playTime,
                              final boolean hadFloatVideo, boolean showCompleteDialog, int seekTime) {

        this.hadFloatVideo = hadFloatVideo;
        videoPlayer.setShowCompleteDialog(showCompleteDialog);
        runOnUiThread(() -> {
            if (hadFloatVideo && TextUtils.isEmpty(url)) {
                changeVideo2Full();
                Log.d(TAG,"changeVideo2Full");
            } else {
                playVideo(url, title, uid, sourceId, playTime, seekTime);
                Log.d(TAG,"playVideo");
            }
        });
    }

    /*
    * 视频窗口暂停对话框猜你喜欢视频播放
    * */

    public void favoritePlay(String sourceId,String title){
        videoPlayer.storeLastPlayTime();
        String url = com.hc.model.BuildConfig.Domain + com.hc.model.BuildConfig.port+"/index.php?m=Home&c=Order&a=authorize&sourceId="
                + sourceId + "&EpgServer=" + GetSXHeZiParam.getInstance(this).getUserEpgServer() + "&token=" + GetSXHeZiParam.getInstance(this).getUserToken();
        new GetRequest().RequestAsync(url, new GetRequestListener() {
            @Override
            public void onResult(String msg) {
                try{
                    JSONObject jsonObject = new JSONObject(msg);
                    if(jsonObject.getInt("code") == 0){
                        String data = jsonObject.getString("data");
                        JSONObject dataObject = new JSONObject(data);
                        if(dataObject.getString("returncode").equals("0")){
                            if(dataObject.has("urls")){
                                JSONArray urls = dataObject.getJSONArray("urls");
                                String playUrl = urls.getJSONObject(0).getString("playurl");
                                playFullVideo(playUrl,title,GetSXHeZiParam.getInstance(MainActivity.this).getUserName(),sourceId,
                                        0,hadFloatVideo,true,0);
                            }else{
                                Log.i(TAG,"没有获取到播放链接");
                            }
                        }else{
                            Log.i(TAG,"鉴权失败");
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void stopFloatVideo() {
        Log.d(TAG,"stopFloatVideo");
        runOnUiThread(this::destroyVideo);
    }

    @Override
    public void webRequest(String tag, String url) {
        Log.d(TAG,"webRequest,url:" + url);
        /*MyRequest stringRequest = new MyRequest(com.android.volley.Request.Method.GET,url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //Log.d(TAG,"访问成功,data:" + response);
                backToWeb(tag,response);
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        Volley.newRequestQueue(this).add(stringRequest);*/
        com.yanzhenjie.nohttp.rest.StringRequest request = new com.yanzhenjie.nohttp.rest.StringRequest(url, RequestMethod.GET);
        CallServer.getInstance().requestString(FJYDConfig.WHAT.AUTH, request, new DefaultCallback<String>() {
            @Override
            public void onStart(int what) {
                Logger.d("开始请求认证");
            }

            @Override
            public void onSuccess(int what, @NonNull String data) {
                Logger.d("请求认证成功:" + data);
                backToWeb(tag,data);
            }

            @Override
            public void onFail(int what, String msg) {
                Logger.d("请求认证失败:" + msg);
            }

            @Override
            public void onError(int what, Throwable e) {
                Logger.e(e, "请求认证发生异常");

            }

            @Override
            public void onFinish(int what) {
                Logger.d("请求认证结束");
            }
        });
    }

    public void backToWeb(String tag,String response){
        if(tag.contains("html")){
            response = "success";
        }
        Log.d(TAG,"response:" + response);
        String base64Response = Base64.encodeToString(response.getBytes(),Base64.DEFAULT);
        base64Response = base64Response.replace("\n","");
        //String base64Response = "eyJjb2RlIjowLCJtc2ciOiJzdWVjY2VzcyIsImRhdGEiOlt7Im1lbnVfaWQiOiI5IiwibWVudV9uYW1lIjoiXHU1YjlkXHU4ZDFkXHU1N2ZhXHU1NzMwIiwiZW5nbGlzaF9uYW1lIjoiYWJvdXQifSx7Im1lbnVfaWQiOiIxIiwibWVudV9uYW1lIjoiXHU2MjExXHU3MjMxXHU3NzBiIiwiZW5nbGlzaF9uYW1lIjoiZmF2b3JpdGUifSx7Im1lbnVfaWQiOiIyIiwibWVudV9uYW1lIjoiXHU1YjY2XHU3N2U1XHU4YmM2IiwiZW5nbGlzaF9uYW1lIjoibGVhcm4ifSx7Im1lbnVfaWQiOiIzIiwibWVudV9uYW1lIjoiXHU1ZGU3XHU1MjM2XHU0ZjVjIiwiZW5nbGlzaF9uYW1lIjoiaGFuZHdvcmsifSx7Im1lbnVfaWQiOiI1IiwibWVudV9uYW1lIjoiXHU3MjMxXHU0ZTkyXHU1MmE4IiwiZW5nbGlzaF9uYW1lIjoicGFpbnRpbmcifSx7Im1lbnVfaWQiOiI3IiwibWVudV9uYW1lIjoiXHU1MmE4XHU3NTNiXHU1N2NlIiwiZW5nbGlzaF9uYW1lIjoiY2FydG9vbiJ9XX0=";
        Log.d(TAG,"javascript:onWebRequestResponse('"+tag+"','"+base64Response+"')");
        AndroidToJS.evaluateJavascript(getWebView(),"javascript:onWebRequestResponse('"+tag+"','"+base64Response+"')");
    }

    class MyRequest extends StringRequest {

        public MyRequest(int method, String url, com.android.volley.Response.Listener<String> listener, @Nullable com.android.volley.Response.ErrorListener errorListener) {
            super(method, url, listener, errorListener);
        }

        @Override
        protected com.android.volley.Response<String> parseNetworkResponse(NetworkResponse response) {
            String parsed;
            try {
                parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers,"utf-8"));
            } catch (UnsupportedEncodingException e) {
                // Since minSdkVersion = 8, we can't call
                // new String(response.data, Charset.defaultCharset())
                // So suppress the warning instead.
                parsed = new String(response.data);
            }
            return com.android.volley.Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
        }

    }

    /**
     * 全屏播放逻辑
     */
    private void playVideo(String url, String title, String uid, String sourceId, long playTime, int seekTime) {
        isFullScreenVideo = true;
        controlDelegate.setFullVideo(true);
        controlDelegate.setLimitTime(playTime);

        if (videoPlayer.getParent() == null) {
            ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
            viewGroup.addView(videoPlayer);
        }
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(screenWidth, screenHeight);
        params.gravity = Gravity.CENTER;
        videoPlayer.setLayoutParams(params);
        int marginWidth = getResources().getDimensionPixelSize(R.dimen.player_bottom_notice_text_margin_left);
        videoPlayer.setMargins(marginWidth,marginWidth,marginWidth,marginWidth);
        videoPlayer.setX(0);
        videoPlayer.setY(0);

        controlDelegate.setUid(uid);
        controlDelegate.setSourceId(sourceId);

        VideoData videoData = new VideoData(url, title);
        videoData.setSeekTimeWhenStart(seekTime);
        videoData.setId(sourceId);
        videoPlayer.start(videoData);
    }

    /**
     * 指定视频窗口位置和大小的播放逻辑
     */
    private void playVideo(String url, String title, String uid, String sourceId, int x, int y, int width, int height, boolean needMute) {
        initVideoParamsIfNoInit(x, y, width, height);

        controlDelegate.setUid(uid);
        controlDelegate.setSourceId(sourceId);

        VideoData videoData = new VideoData(url, title);
        videoData.setMute(needMute);
        videoData.setId(sourceId);
        videoPlayer.start(videoData);
        videoPlayer.clearFocus();
        videoPlayer.setMargins(0,0,0,0);
        getWebView().requestFocus();
    }

    private void initVideoParamsIfNoInit(int x, int y, int width, int height) {
        float widthDensity = screenWidth/baseScreenWidth;
        float heightDensity = screenHeight/baseScreenHeight;
        floatVideoWidth = (int)(width*widthDensity);
        floatVideoHeight = (int)(height*heightDensity);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.width = floatVideoWidth;
        params.height = floatVideoHeight;
        videoPlayer.setLayoutParams(params);

        floatVideoX = (int)(x*widthDensity);
        floatVideoY = (int)(y*heightDensity);
        videoPlayer.setX(floatVideoX);
        videoPlayer.setY(floatVideoY);

        if (videoPlayer.getParent() == null) {
            ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
            viewGroup.addView(videoPlayer);
        }
    }

    /**
     * 小窗转全屏
     */
    private void changeVideo2Full() {
        if (isFullScreenVideo) {
            return;
        }

        if (videoPlayer != null && videoPlayer.getParent() != null) {
            isFullScreenVideo = true;
            controlDelegate.setFullVideo(true);
            getWebView().clearFocus();
            videoPlayer.requestFocus();
            animChanged(floatVideoX, 0, floatVideoY, 0,
                    floatVideoWidth, screenWidth,
                    floatVideoHeight, screenHeight,true);
            videoPlayer.pauseOrPlay(true);
            videoPlayer.setAudioMute(false);
        }
    }

    /**
     * 全屏转小窗
     */
    private void changeVideo2Float() {
        if (!isFullScreenVideo) {
            return;
        }

        if (videoPlayer != null && videoPlayer.getWidth() == screenWidth) {
            AndroidToJS.evaluateJavascript(getWebView(), AndroidToJS.JS_EVENT_PLAY_FINISH);
            Log.d(TAG,"PLAY_FINISH");
        }
        if (videoPlayer != null) {
            isFullScreenVideo = false;
            controlDelegate.setFullVideo(false);
            videoPlayer.hideSeekContainer();
            videoPlayer.pauseOrPlay(true);
            videoPlayer.clearFocus();
            getWebView().requestFocus();
            animChanged(0, floatVideoX, 0, floatVideoY,
                    screenWidth, floatVideoWidth,
                    screenHeight, floatVideoHeight,false);

           /* destroyVideo();

            initMediaPlayer();

            playVideo(currentFloatInfo.url,currentFloatInfo.title,currentFloatInfo.uid,currentFloatInfo.sourceId,
                    currentFloatInfo.x,currentFloatInfo.y,currentFloatInfo.width,currentFloatInfo.height,
                    currentFloatInfo.needMute);*/

        }
    }

    private void animChanged(int fromX, int toX, int fromY, int toY, int fromWidth, int toWidth, int fromHeight, int toHeight,boolean isFullScreenVideo) {
        ValueAnimator animator = new ValueAnimator();
        animator.setValues(
                PropertyValuesHolder.ofFloat("x", fromX, toX),
                PropertyValuesHolder.ofFloat("y", fromY, toY),
                PropertyValuesHolder.ofInt("width", fromWidth, toWidth),
                PropertyValuesHolder.ofInt("height", fromHeight, toHeight)
        );
        animator.setDuration(200);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(valueAnimator -> {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.width = (int) valueAnimator.getAnimatedValue("width");
            params.height = (int) valueAnimator.getAnimatedValue("height");

            videoPlayer.setLayoutParams(params);
            if(isFullScreenVideo){
                int marginWidth = getResources().getDimensionPixelSize(R.dimen.player_bottom_notice_text_margin_left);
                videoPlayer.setMargins(marginWidth,marginWidth,marginWidth,marginWidth);
            }else{
                videoPlayer.setMargins(0,0,0,0);
            }

            videoPlayer.setX((Float) valueAnimator.getAnimatedValue("x"));
            videoPlayer.setY((Float) valueAnimator.getAnimatedValue("y"));
        });
        animator.start();
    }

    private void destroyVideo() {
        try{
            isFullScreenVideo = false;
            controlDelegate.setFullVideo(false);
            if(videoPlayer == null){
                return;
            }
            if (videoPlayer != null && videoPlayer.getWidth() == screenWidth) {
                AndroidToJS.evaluateJavascript(getWebView(), AndroidToJS.JS_EVENT_PLAY_FINISH);
                Log.d(TAG,"PLAY_FINISH");
            }
            videoPlayer.hideSeekContainer();
            videoPlayer.onVideoPause();
            videoPlayer.onVideoDestroy();

            ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
            viewGroup.removeView(videoPlayer);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
    * 显示收藏成功和取消收藏成功,add by 许林
    * */
    public void collectSuccess(){
        showCollectSuccessImage();
    }

    public void cancelCollectSuccess(){
        showCancelCollectImage();
    }

    /*private String getUserName(){
        try{
            Uri queryUri = Uri.parse("content://stbauthinfo/authentication/");
            Cursor cursor = getContentResolver().query(queryUri,new String[]{"value"},"name='username'",null,null);
            cursor.moveToFirst();
            String userName = cursor.getString(cursor.getColumnIndexOrThrow("value"));
            Log.d(TAG,"userName:" + userName);
            cursor.close();
            return userName;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    private String getUserToken(){
        try{
            Uri queryUri = Uri.parse("content://stbauthinfo/authentication/");
            Cursor cursor = getContentResolver().query(queryUri,new String[]{"value"},"name='user_token'",null,null);
            cursor.moveToFirst();
            String userToken = cursor.getString(cursor.getColumnIndexOrThrow("value"));
            //userToken = "Se48ja9jiqjq-Stjhd4izfd";
            Log.d(TAG,"userToken:" + userToken);
            cursor.close();
            return userToken;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    private String getUserEpgServer(){
        try{
            Uri queryUri = Uri.parse("content://stbauthinfo/authentication/");
            Cursor cursor = getContentResolver().query(queryUri,new String[]{"value"},"name='epg_server'",null,null);
            cursor.moveToFirst();
            String epgServer = cursor.getString(cursor.getColumnIndexOrThrow("value"));
            //epgServer = "http://111.20.42.163:33200";
            Log.d(TAG,"epgServer:" + epgServer);
            cursor.close();
            return epgServer;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }*/

}
