summaryrefslogtreecommitdiff
path: root/core/java/com/nvidia/NvWhitelistService.java
blob: dfed390ccd91692da94152e110140212d15f3178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.nvidia;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;

import com.android.internal.R;

import java.util.ArrayList;
import java.io.IOException;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.xmlpull.v1.XmlPullParserException;

public class NvWhitelistService {
    private static final String TAG = "NvWhitelistService";
    private JSONArray mWhiteListArray;
    private Context mContext;

    public NvWhitelistService(Context context) {
        Context appContext = context.getApplicationContext();
        mContext = appContext != null ? appContext : context;
        mWhiteListArray =
                parseXml(mContext.getResources().getXml(R.xml.tv_launcher_app_white_list));
    }

    public boolean isWhiteApp(String pkgName) {
        if (mWhiteListArray == null) return false;

        for (int i = 0; i < mWhiteListArray.length(); i++) {
            try {
                if (pkgName.equals(mWhiteListArray.getJSONObject(i).getString("packageName"))) {
                    return true;
                }
            } catch (JSONException ex) {
                Log.w(TAG, ex.getMessage());
            }
        }

        return false;
    }

    public boolean isTvGame(String pkgName) {
        if (mWhiteListArray == null) return false;

        for (int i = 0; i < mWhiteListArray.length(); i++) {
            try {
                if (mWhiteListArray.getJSONObject(i).getString("packageName").equals(pkgName) &&
                        mWhiteListArray.getJSONObject(i).getString("isGame").equals("true")) {
                    return true;
                }
            } catch (JSONException ex) {
                Log.w(TAG, ex.getMessage());
            }
        }

        return false;
    }

    public String getBannerName(String pkgName) {
        for (int i = 0; i < mWhiteListArray.length(); i++) {
            try {
                if (mWhiteListArray.getJSONObject(i).getString("packageName").equals(pkgName)) {
                    return mWhiteListArray.getJSONObject(i).getString("bannerName");
                }
            } catch (JSONException ex) {
                Log.w(TAG, ex.getMessage());
            }
        }

        return "";
    }

    private JSONArray parseXml(XmlResourceParser xmlParser) {
        if (xmlParser == null) return null;

        JSONObject jsonObj = null;
        ArrayList<JSONObject> widgetConfigs = new ArrayList<>();
        try {
            int type = xmlParser.getEventType();
            while (type != XmlResourceParser.END_DOCUMENT) {
                switch (type) {
                    case XmlResourceParser.START_TAG:
                        if (xmlParser.getName().equals("app")) {
                            jsonObj = new JSONObject();
                            for (int i = 0; i < xmlParser.getAttributeCount(); i++) {
                                jsonObj.put(xmlParser.getAttributeName(i),
                                        xmlParser.getAttributeValue(i));
                            }
                            break;
                        }
                        break;
                    case XmlResourceParser.END_TAG:
                        if (xmlParser.getName().equals("app") && jsonObj != null) {
                            widgetConfigs.add(jsonObj);
                            break;
                        }
                        break;
                    default:
                        break;
                }
                type = xmlParser.next();
            }
        } catch (IOException | JSONException | XmlPullParserException e) {
            e.printStackTrace();
        }

        return new JSONArray(widgetConfigs);
    }
}