Search in sources :

Example 1 with KcaUtils.setPreferences

use of com.antest1.kcanotify.KcaUtils.setPreferences in project kcanotify by antest1.

the class UpdateCheckActivity method downloadGameData.

private void downloadGameData() {
    final Call<String> down_gamedata = downloader.getGameData("recent");
    down_gamedata.enqueue(new retrofit2.Callback<String>() {

        @Override
        public void onResponse(Call<String> call, retrofit2.Response<String> response) {
            JsonObject response_data = new JsonObject();
            try {
                if (response.body() != null) {
                    response_data = new JsonParser().parse(response.body()).getAsJsonObject();
                    String kca_version = KcaUtils.getStringPreferences(getApplicationContext(), PREF_KCA_VERSION);
                    String server_kca_version = response.headers().get("X-Api-Version");
                    Log.e("KCA", "api_version: " + server_kca_version);
                    if (kca_version == null || compareVersion(server_kca_version, kca_version)) {
                        dbHelper.putValue(DB_KEY_STARTDATA, response_data.toString());
                        KcaApiData.getKcGameData(response_data.getAsJsonObject("api_data"));
                        KcaUtils.setPreferences(getApplicationContext(), PREF_KCA_DATA_VERSION, server_kca_version);
                        KcaApiData.setDataLoadTriggered();
                        Toast.makeText(getApplicationContext(), getStringWithLocale(R.string.sa_getupdate_finished), Toast.LENGTH_LONG).show();
                        JsonObject gamedata = gamedata_info.get(0);
                        String latest_gd_v = latest_gamedata_version;
                        gamedata.addProperty("version", latest_gd_v);
                        gamedata.addProperty("version_str", getVersionString(server_kca_version, latest_gd_v));
                        gamedata.addProperty("highlight", !KcaUtils.compareVersion(server_kca_version, latest_gd_v));
                        gamedata_adapter.setContext(getApplicationContext());
                        gamedata_adapter.setListItem(gamedata_info);
                        gamedata_adapter.notifyDataSetChanged();
                    } else {
                        Toast.makeText(getApplicationContext(), getStringWithLocale(R.string.kca_toast_inconsistent_data), Toast.LENGTH_LONG).show();
                        ;
                    }
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error: not valid data.", Toast.LENGTH_LONG).show();
                ;
                dbHelper.recordErrorLog(ERROR_TYPE_SETTING, "fairy_queue", "", "", getStringFromException(e));
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            if (KcaUtils.checkOnline(getApplicationContext())) {
                Toast.makeText(getApplicationContext(), KcaUtils.format(getStringWithLocale(R.string.sa_getupdate_servererror), t.getMessage()), Toast.LENGTH_LONG).show();
                ;
                dbHelper.recordErrorLog(ERROR_TYPE_SETTING, "fairy_queue", "", "", t.getMessage());
            }
        }
    });
}
Also used : JsonObject(com.google.gson.JsonObject) KcaUtils.getStringFromException(com.antest1.kcanotify.KcaUtils.getStringFromException) JsonParser(com.google.gson.JsonParser)

Example 2 with KcaUtils.setPreferences

use of com.antest1.kcanotify.KcaUtils.setPreferences in project kcanotify by antest1.

the class InitStartActivity method onCreate.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_init_download);
    Log.e("KCA-DA", "created");
    sendUserAnalytics(getApplicationContext(), START_APP, null);
    Intent mainIntent = getIntent();
    reset_flag = mainIntent.getBooleanExtra(ACTION_RESET, false);
    is_destroyed = false;
    PreferenceManager.setDefaultValues(this, R.xml.pref_settings, false);
    setDefaultPreferences();
    dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION);
    downloader = KcaUtils.getInfoDownloader(getApplicationContext());
    KcaApiData.setDBHelper(dbHelper);
    appname = findViewById(R.id.app_title);
    appname.setText(getStringWithLocale(R.string.app_name));
    appversion = findViewById(R.id.app_version);
    appversion.setText(getString(R.string.app_version));
    appmessage = findViewById(R.id.app_message);
    appmessage.setText("");
    appfront = findViewById(R.id.app_icon);
    int img_id = (int) (Math.random() * 4);
    appfront.setImageResource(R.mipmap.main_image);
    int fairy_id = Integer.parseInt(getStringPreferences(getApplicationContext(), PREF_FAIRY_ICON));
    if (!FAIRY_SPECIAL_FLAG && fairy_id >= FAIRY_SPECIAL_PREFIX) {
        setPreferences(getApplicationContext(), PREF_FAIRY_ICON, 0);
    }
    // Initialize resources
    setPreferences(getApplicationContext(), PREF_DATALOAD_ERROR_FLAG, false);
    loadDefaultAsset();
    validated_flag = KcaUtils.validateResourceFiles(getApplicationContext(), dbHelper);
    setPreferences(getApplicationContext(), PREF_DATALOAD_ERROR_FLAG, false);
    findViewById(R.id.init_layout).setOnClickListener(v -> {
        if (validated_flag && !reset_flag) {
            startMainActivity(false);
            is_skipped = true;
        }
    });
    new Thread(() -> {
        runOnUiThread(() -> appmessage.setText("Loading Translation Data..."));
        loadTranslationData(getApplicationContext());
        runOnUiThread(() -> appmessage.setText("Loading KanColle Game Data..."));
        int setDefaultGameDataResult = KcaUtils.setDefaultGameData(getApplicationContext(), dbHelper);
        if (setDefaultGameDataResult != 1) {
            runOnUiThread(() -> Toast.makeText(getApplicationContext(), "error loading game data", Toast.LENGTH_LONG).show());
        }
        runOnUiThread(() -> appmessage.setText("Checking External Filter..."));
        boolean allow_ext = KcaUtils.getBooleanPreferences(getApplicationContext(), PREF_ALLOW_EXTFILTER);
        List<String> result = KcaVpnData.setExternalFilter(allow_ext);
        boolean is_default_written = KcaUtils.getStringPreferences(getApplicationContext(), PREF_DEFAULT_APIVER).equals(BuildConfig.VERSION_NAME);
        if (!is_default_written) {
            runOnUiThread(() -> appmessage.setText("Writing Default Data..."));
            String internal_kca_version = getString(R.string.default_gamedata_version);
            try {
                InputStream api_ais = getAssets().open("api_start2");
                byte[] bytes = gzipdecompress(ByteStreams.toByteArray(api_ais));
                String asset_start2_data = new String(bytes);
                dbHelper.putValue(DB_KEY_STARTDATA, asset_start2_data);
                KcaUtils.setPreferences(getApplicationContext(), PREF_DEFAULT_APIVER, BuildConfig.VERSION_NAME);
                KcaUtils.setPreferences(getApplicationContext(), PREF_KCA_VERSION, internal_kca_version);
                KcaUtils.setPreferences(getApplicationContext(), PREF_KCA_DATA_VERSION, internal_kca_version);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // main update check
        if (!KcaUtils.checkOnline(getApplicationContext()) || !getBooleanPreferences(getApplicationContext(), PREF_CHECK_UPDATE_START)) {
            startMainActivity(true);
        } else {
            if (is_skipped)
                return;
            runOnUiThread(() -> appmessage.setText("Checking Updates..."));
            String currentVersion = BuildConfig.VERSION_NAME;
            final Call<String> rv_data = downloader.getRecentVersion();
            String response = getResultFromCall(rv_data);
            new_resversion = -1;
            fairy_flag = 0;
            JsonObject response_data = new JsonObject();
            try {
                if (response != null) {
                    response_data = new JsonParser().parse(response).getAsJsonObject();
                }
            } catch (Exception e) {
                dbHelper.recordErrorLog(ERROR_TYPE_MAIN, "version_check", "", "", getStringFromException(e));
            }
            Log.e("KCA", response_data.toString());
            if (response_data.has("kc_maintenance")) {
                dbHelper.putValue(DB_KEY_KCMAINTNC, response_data.get("kc_maintenance").toString());
            }
            if (response_data.has("version")) {
                String recentVersion = response_data.get("version").getAsString();
                if (!compareVersion(currentVersion, recentVersion)) {
                    // True if latest
                    JsonObject data = response_data;
                    if (!is_skipped) {
                        runOnUiThread(() -> {
                            AlertDialog.Builder alertDialog = getUpdateAlertDialog(recentVersion, data);
                            AlertDialog alert = alertDialog.create();
                            alert.setIcon(R.mipmap.ic_launcher);
                            alert.setTitle(getStringWithLocale(R.string.sa_checkupdate_dialogtitle));
                            if (!InitStartActivity.this.isFinishing()) {
                                alert.show();
                            }
                        });
                    }
                } else {
                    dataCheck(response_data);
                }
            }
        }
    }).start();
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) Call(retrofit2.Call) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) Intent(android.content.Intent) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) KcaUtils.getStringFromException(com.antest1.kcanotify.KcaUtils.getStringFromException) IOException(java.io.IOException) List(java.util.List) ArrayList(java.util.ArrayList) JsonParser(com.google.gson.JsonParser)

Example 3 with KcaUtils.setPreferences

use of com.antest1.kcanotify.KcaUtils.setPreferences in project kcanotify by antest1.

the class KcaInspectorActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inspector);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(getResources().getString(R.string.action_inspector));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    listViewItemList = new ArrayList<>();
    adapter = new KcaInspectViewAdpater();
    dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION);
    questTracker = new KcaQuestTracker(getApplicationContext(), null, KCANOTIFY_QTDB_VERSION);
    packetLogger = new KcaPacketLogger(getApplicationContext(), null, KCANOTIFY_PACKETLOG_VERSION);
    for (String db_key : DB_KEY_ARRAY) {
        String db_value = dbHelper.getValue(db_key);
        if (db_value == null)
            db_value = "<null>";
        else if (db_value.length() > 100) {
            db_value = db_value.substring(0, 100).concat(KcaUtils.format("... (%d)", db_value.length()));
        }
        listViewItemList.add(new AbstractMap.SimpleEntry<>(DB_PREFIX.concat(db_key), db_value));
    }
    String questlist_data = dbHelper.getQuestListData();
    String questlist_view = questlist_data.replace("\n", ", ");
    if (questlist_view.length() > 100) {
        questlist_view = questlist_view.substring(0, 100).concat(KcaUtils.format("... (%d)", questlist_data.length()));
    }
    listViewItemList.add(new AbstractMap.SimpleEntry<>(DQ_PREFIX.concat("quest_data"), questlist_view));
    String questtrack_data = questTracker.getQuestTrackerDump();
    listViewItemList.add(new AbstractMap.SimpleEntry<>(QT_PREFIX.concat("tracked_data"), questtrack_data));
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listViewItemList.add(new AbstractMap.SimpleEntry<>(SPREF_PREFIX.concat(PREF_VPN_ENABLED), String.valueOf(prefs.getBoolean(PREF_VPN_ENABLED, false))));
    listViewItemList.add(new AbstractMap.SimpleEntry<>(SPREF_PREFIX.concat(PREF_SVC_ENABLED), String.valueOf(prefs.getBoolean(PREF_SVC_ENABLED, false))));
    for (String pref_key : PREFS_LIST) {
        String pref_value = "";
        try {
            if (PREFS_BOOLEAN_LIST.contains(pref_key)) {
                pref_value = String.valueOf(getBooleanPreferences(getApplicationContext(), pref_key));
            } else {
                pref_value = getStringPreferences(getApplicationContext(), pref_key);
            }
        } catch (Exception e) {
            dbHelper.recordErrorLog(ERROR_TYPE_SETTING, "pref", "", "", getStringFromException(e));
        }
        listViewItemList.add(new AbstractMap.SimpleEntry<>(PREF_PREFIX.concat(pref_key), pref_value));
    }
    adapter.setListViewItemList(listViewItemList);
    listview = findViewById(R.id.inspector_listview);
    listview.setAdapter(adapter);
    packetlogEnable = findViewById(R.id.packetlog_enable);
    packetlogEnable.setChecked(getBooleanPreferences(getApplicationContext(), PREF_PACKET_LOG));
    packetlogEnable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            KcaUtils.setPreferences(getApplicationContext(), PREF_PACKET_LOG, isChecked);
        }
    });
    packletlogClear = findViewById(R.id.packetlog_clear);
    packletlogClear.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            packetLogger.clear();
        }
    });
    packetlogDump = findViewById(R.id.packetlog_dump);
    packetlogDump.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean result = packetLogger.dump(getApplicationContext());
            Toast.makeText(getApplicationContext(), String.valueOf(result), Toast.LENGTH_LONG).show();
        }
    });
}
Also used : SharedPreferences(android.content.SharedPreferences) View(android.view.View) ListView(android.widget.ListView) KcaUtils.getStringFromException(com.antest1.kcanotify.KcaUtils.getStringFromException) AbstractMap(java.util.AbstractMap) CompoundButton(android.widget.CompoundButton)

Aggregations

KcaUtils.getStringFromException (com.antest1.kcanotify.KcaUtils.getStringFromException)3 JsonObject (com.google.gson.JsonObject)2 JsonParser (com.google.gson.JsonParser)2 SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 View (android.view.View)1 CompoundButton (android.widget.CompoundButton)1 ListView (android.widget.ListView)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Call (retrofit2.Call)1