Search in sources :

Example 11 with KCANOTIFY_DB_VERSION

use of com.antest1.kcanotify.KcaConstants.KCANOTIFY_DB_VERSION in project kcanotify by antest1.

the class ResourceLogActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resourcelog);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(getStringWithLocale(R.string.action_reslog));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION);
    resourceLogger = new KcaResourceLogger(getApplicationContext(), null, KCANOTIFY_RESOURCELOG_VERSION);
    KcaApiData.setDBHelper(dbHelper);
    tabLayout = findViewById(R.id.reslog_tab);
    tabLayout.addTab(tabLayout.newTab().setText(getStringWithLocale(R.string.reslog_label_resource)));
    tabLayout.addTab(tabLayout.newTab().setText(getStringWithLocale(R.string.reslog_label_consumable)));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    export_msg = findViewById(R.id.export_msg);
    viewPager = findViewById(R.id.reslog_pager);
    pageAdapter = new KcaResourceLogPageAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pageAdapter);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            tab_position = position;
            tabLayout.setScrollPosition(position, positionOffset, true);
            Log.e("KCA", "onPageScrolled " + position);
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tab_position = tab.getPosition();
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            tab_position = tab.getPosition();
            viewPager.setCurrentItem(tab.getPosition());
        }
    });
    showhide_btn = findViewById(R.id.reslog_showhide);
    showhide_btn.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.black), PorterDuff.Mode.MULTIPLY);
    showhide_btn.setImageResource(R.mipmap.ic_arrow_down);
    showhide_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            is_hidden = !is_hidden;
            if (is_hidden) {
                showhide_btn.setImageResource(R.mipmap.ic_arrow_up);
            } else {
                showhide_btn.setImageResource(R.mipmap.ic_arrow_down);
            }
            pageAdapter.notifyDataSetChanged();
        }
    });
    start_date = findViewById(R.id.reslog_date_start);
    start_date.setOnClickListener(dateViewListener);
    end_date = findViewById(R.id.reslog_date_end);
    end_date.setOnClickListener(dateViewListener);
    interval_d = findViewById(R.id.reslog_interval_day);
    interval_d.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (interval_value != INTERVAL_1D) {
                interval_value = INTERVAL_1D;
                setFragmentChartInfo(interval_value);
                interval_select.setSelection(INTERVAL_1D);
            } else {
                pageAdapter.notifyDataSetChanged();
            }
        }
    });
    interval_w = findViewById(R.id.reslog_interval_week);
    interval_w.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (interval_value != INTERVAL_1W) {
                interval_value = INTERVAL_1W;
                setFragmentChartInfo(interval_value);
                interval_select.setSelection(INTERVAL_1W);
            } else {
                pageAdapter.notifyDataSetChanged();
            }
        }
    });
    interval_m = findViewById(R.id.reslog_interval_month);
    interval_m.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (interval_value != INTERVAL_1M) {
                interval_value = INTERVAL_1M;
                setFragmentChartInfo(interval_value);
                interval_select.setSelection(INTERVAL_1M);
            } else {
                pageAdapter.notifyDataSetChanged();
            }
        }
    });
    interval_select = findViewById(R.id.reslog_date_interval);
    final ArrayAdapter<CharSequence> interval_adapter = ArrayAdapter.createFromResource(this, R.array.time_interval_array, R.layout.spinner_item_14dp);
    interval_adapter.setDropDownViewResource(R.layout.spinner_dropdown_item_14dp);
    interval_select.setAdapter(interval_adapter);
    interval_select.setSelection(INTERVAL_1D);
    interval_select.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            Log.e("KCA-RL", "onItemSelected");
            interval_value = position;
            setFragmentChartInfo(interval_value);
            KcaResourcelogItemAdpater.setListViewItemList(convertData(resourceLog));
            pageAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });
    long current_time = System.currentTimeMillis();
    current_time = KcaUtils.getCurrentDateTimestamp(current_time);
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
    String[] time_list = dateFormat.format(new Date(current_time)).split("\\-");
    int year = Integer.parseInt(time_list[2]);
    int month = Integer.parseInt(time_list[0]);
    start_timestamp = current_time - DAY_MILLISECOND * KcaUtils.getLastDay(year, month == 1 ? 12 : month - 1);
    end_timestamp = current_time + DAY_MILLISECOND - 1;
    start_date.setText(convertMillsToDate(start_timestamp));
    end_date.setText(convertMillsToDate(end_timestamp));
    resourceLog = resourceLogger.getResourceLogInRange(start_timestamp, end_timestamp);
    KcaResourcelogItemAdpater.setListViewItemList(convertData(resourceLog));
    pageAdapter.notifyDataSetChanged();
}
Also used : ViewPager(androidx.viewpager.widget.ViewPager) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) Date(java.util.Date) DropLogActivity.convertMillsToDate(com.antest1.kcanotify.DropLogActivity.convertMillsToDate) TabLayout(com.google.android.material.tabs.TabLayout) AdapterView(android.widget.AdapterView) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with KCANOTIFY_DB_VERSION

use of com.antest1.kcanotify.KcaConstants.KCANOTIFY_DB_VERSION in project kcanotify by antest1.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vpn_main);
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    assetManager = getAssets();
    dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(R.string.app_name);
    prefs.edit().putBoolean(PREF_SVC_ENABLED, KcaService.getServiceStatus()).apply();
    prefs.edit().putBoolean(PREF_VPN_ENABLED, KcaVpnService.checkOn()).apply();
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    downloader = KcaUtils.getInfoDownloader(getApplicationContext());
    int sniffer_mode = Integer.parseInt(getStringPreferences(getApplicationContext(), PREF_SNIFFER_MODE));
    vpnbtn = findViewById(R.id.vpnbtn);
    vpnbtn.setTextOff(getStringWithLocale(R.string.ma_vpn_toggleoff));
    vpnbtn.setTextOn(getStringWithLocale(R.string.ma_vpn_toggleon));
    vpnbtn.setText("PASSIVE");
    vpnbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                JsonObject statProperties = new JsonObject();
                try {
                    final Intent prepare = VpnService.prepare(MainActivity.this);
                    if (prepare == null) {
                        // Log.i(TAG, "Prepare done");
                        onActivityResult(REQUEST_VPN, RESULT_OK, null);
                    } else {
                        startActivityForResult(prepare, REQUEST_VPN);
                    }
                    statProperties.addProperty("is_success", true);
                } catch (Throwable ex) {
                    // Prepare failed
                    statProperties.addProperty("is_success", false);
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
                sendUserAnalytics(getApplicationContext(), START_SNIFFER, statProperties);
            } else {
                KcaVpnService.stop(VPN_STOP_REASON, MainActivity.this);
                prefs.edit().putBoolean(PREF_VPN_ENABLED, false).apply();
                sendUserAnalytics(getApplicationContext(), END_SNIFFER, null);
            }
        }
    });
    if (sniffer_mode == SNIFFER_ACTIVE) {
        vpnbtn.setEnabled(true);
    } else {
        vpnbtn.setEnabled(false);
    }
    Intent serviceIntent = new Intent(MainActivity.this, KcaService.class);
    svcbtn = findViewById(R.id.svcbtn);
    svcbtn.setTextOff(getStringWithLocale(R.string.ma_svc_toggleoff));
    svcbtn.setTextOn(getStringWithLocale(R.string.ma_svc_toggleon));
    svcbtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if (isChecked) {
            if (!prefs.getBoolean(PREF_SVC_ENABLED, false)) {
                sendUserAnalytics(getApplicationContext(), START_SERVICE, null);
                loadTranslationData(getApplicationContext());
                serviceIntent.setAction(KcaService.KCASERVICE_START);
                if (Build.VERSION.SDK_INT >= 26) {
                    startForegroundService(serviceIntent);
                } else {
                    startService(serviceIntent);
                }
            }
        } else {
            serviceIntent.setAction(KcaService.KCASERVICE_STOP);
            startService(serviceIntent);
            sendUserAnalytics(getApplicationContext(), END_SERVICE, null);
            prefs.edit().putBoolean(PREF_SVC_ENABLED, false).apply();
        }
    });
    kcbtn = findViewById(R.id.kcbtn);
    kcbtn.setOnClickListener(v -> {
        String kcApp = getStringPreferences(getApplicationContext(), PREF_KC_PACKAGE);
        Intent kcIntent = getKcIntent(getApplicationContext());
        boolean is_kca_installed = false;
        if (!BuildConfig.DEBUG)
            is_kca_installed = (kcIntent != null);
        JsonObject statProperties = new JsonObject();
        statProperties.addProperty("browser", kcApp);
        statProperties.addProperty("sniffer", sniffer_mode);
        statProperties.addProperty("enabled", is_kca_installed ? 1 : 0);
        sendUserAnalytics(getApplicationContext(), RUN_KANCOLLE, statProperties);
        if (is_kca_installed) {
            kcIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(kcIntent);
            finish();
        } else {
            if (kcApp.equals(KC_PACKAGE_NAME)) {
                Toast.makeText(getApplicationContext(), getString(R.string.ma_toast_kancolle_not_installed), Toast.LENGTH_LONG).show();
            } else if (kcApp.equals(GOTO_PACKAGE_NAME)) {
                Toast.makeText(getApplicationContext(), getString(R.string.ma_toast_gotobrowser_not_installed), Toast.LENGTH_LONG).show();
            }
        }
    });
    kctoolbtn = findViewById(R.id.kcatoolbtn);
    kctoolbtn.setOnClickListener(v -> {
        Intent intent = new Intent(MainActivity.this, ToolsActivity.class);
        startActivity(intent);
    });
    kctoolbtn.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.white), PorterDuff.Mode.SRC_ATOP);
    kcafairybtn = findViewById(R.id.kcafairybtn);
    boolean is_random_fairy = getBooleanPreferences(getApplicationContext(), PREF_FAIRY_RANDOM);
    if (is_random_fairy) {
        kcafairybtn.setImageResource(R.mipmap.ic_help);
    } else {
        String fairyIdValue = getStringPreferences(getApplicationContext(), PREF_FAIRY_ICON);
        String fairyPath = "noti_icon_".concat(fairyIdValue);
        KcaUtils.setFairyImageFromStorage(getApplicationContext(), fairyPath, kcafairybtn, 24);
    }
    kcafairybtn.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.white), PorterDuff.Mode.SRC_ATOP);
    kcafairybtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(getApplicationContext())) {
            // Can not draw overlays: pass
            } else if (KcaService.getServiceStatus() && sHandler != null) {
                Bundle bundle = new Bundle();
                bundle.putString("url", KCA_API_FAIRY_RETURN);
                bundle.putString("data", "");
                Message sMsg = sHandler.obtainMessage();
                sMsg.setData(bundle);
                sHandler.sendMessage(sMsg);
            }
        }
    });
    textWarn = findViewById(R.id.textMainWarn);
    textWarn.setVisibility(View.GONE);
    String main_html = "";
    try {
        String locale = getStringPreferences(getApplicationContext(), PREF_KCA_LANGUAGE);
        InputStream ais = assetManager.open("main-".concat(getResourceLocaleCode(locale)).concat(".html"));
        byte[] bytes = ByteStreams.toByteArray(ais);
        main_html = new String(bytes);
    } catch (IOException e) {
        main_html = "Error loading html file.";
    }
    textMainUpdate = findViewById(R.id.textMainUpdate);
    textMainUpdate.setOnClickListener(v -> {
        startActivity(new Intent(this, UpdateCheckActivity.class));
    });
    textDescription = findViewById(R.id.textDescription);
    Spanned fromHtml = HtmlCompat.fromHtml(getApplicationContext(), main_html, 0);
    textDescription.setMovementMethod(LinkMovementMethod.getInstance());
    textDescription.setText(fromHtml);
    // Linkify.addLinks(textDescription, Linkify.WEB_URLS);
    backPressCloseHandler = new BackPressCloseHandler(this);
    textMaintenance = findViewById(R.id.textMaintenance);
    String maintenanceInfo = dbHelper.getValue(DB_KEY_KCMAINTNC);
    if (maintenanceInfo != null && maintenanceInfo.trim().length() > 0) {
        try {
            JsonArray maintenance_data = (new JsonParser().parse(maintenanceInfo)).getAsJsonArray();
            String mt_start = maintenance_data.get(0).getAsString();
            String mt_end = maintenance_data.get(1).getAsString();
            if (!mt_start.equals("")) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.US);
                Date start_date = df.parse(mt_start);
                Date end_date = df.parse(mt_end);
                SimpleDateFormat out_df = df;
                if (Build.VERSION.SDK_INT >= 18) {
                    out_df = new SimpleDateFormat(DateFormat.getBestDateTimePattern(Locale.getDefault(), "EEE, dd MMM yyyy HH:mm Z"), Locale.getDefault());
                }
                boolean is_passed = end_date.getTime() < System.currentTimeMillis();
                boolean before_maintenance = System.currentTimeMillis() < start_date.getTime();
                if (before_maintenance) {
                    textMaintenance.setText(KcaUtils.format(getStringWithLocale(R.string.ma_nextmaintenance), out_df.format(start_date)));
                } else if (!is_passed) {
                    textMaintenance.setText(KcaUtils.format(getStringWithLocale(R.string.ma_endmaintenance), out_df.format(end_date)));
                }
                textMaintenance.setVisibility(View.VISIBLE);
            } else {
                textMaintenance.setVisibility(View.GONE);
            }
        } catch (ParseException | IllegalStateException e) {
            textMaintenance.setText(getStringFromException(e));
            textMaintenance.setVisibility(View.VISIBLE);
        }
    } else {
        textMaintenance.setVisibility(View.GONE);
    }
    String locale = getStringPreferences(getApplicationContext(), PREF_KCA_LANGUAGE);
    ImageView specialImage = findViewById(R.id.special_image);
    specialImage.setImageResource(R.mipmap.special_image);
    specialImage.setVisibility(View.GONE);
    specialImage.setOnClickListener(v -> v.setVisibility(View.GONE));
    textSpecial = findViewById(R.id.textSpecial);
    textSpecial.setText(getStringWithLocale(R.string.special_message));
    textSpecial.setOnClickListener(v -> {
        specialImage.setImageResource(R.mipmap.special_image);
        specialImage.setVisibility(View.VISIBLE);
        sendUserAnalytics(getApplicationContext(), OPEN_PIC, null);
    });
    textSpecial2 = findViewById(R.id.textSpecial2);
    textSpecial2.setText(getStringWithLocale(R.string.notification_message));
    textSpecial2.setOnClickListener(v -> {
        String locale1 = getStringPreferences(getApplicationContext(), PREF_KCA_LANGUAGE);
        String locale_code = getResourceLocaleCode(locale1);
        String url = KcaUtils.format("http://luckyjervis.com/noti.html", locale_code);
        CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
        intentBuilder.setShowTitle(true);
        intentBuilder.setToolbarColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
        intentBuilder.enableUrlBarHiding();
        final CustomTabsIntent customTabsIntent = intentBuilder.build();
        final List<ResolveInfo> customTabsApps = getPackageManager().queryIntentActivities(customTabsIntent.intent, 0);
        if (customTabsApps.size() > 0) {
            customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
        } else {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(browserIntent);
        }
    });
}
Also used : Message(android.os.Message) JsonObject(com.google.gson.JsonObject) ResolveInfo(android.content.pm.ResolveInfo) ImageView(android.widget.ImageView) JsonParser(com.google.gson.JsonParser) Bundle(android.os.Bundle) InputStream(java.io.InputStream) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) Intent(android.content.Intent) KcaUtils.getKcIntent(com.antest1.kcanotify.KcaUtils.getKcIntent) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Spanned(android.text.Spanned) Date(java.util.Date) JsonArray(com.google.gson.JsonArray) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) CompoundButton(android.widget.CompoundButton)

Aggregations

KcaUtils.getStringFromException (com.antest1.kcanotify.KcaUtils.getStringFromException)9 TextView (android.widget.TextView)7 Point (android.graphics.Point)6 Display (android.view.Display)6 WindowManager (android.view.WindowManager)6 View (android.view.View)5 Intent (android.content.Intent)4 JsonObject (com.google.gson.JsonObject)4 SuppressLint (android.annotation.SuppressLint)2 Context (android.content.Context)2 Bundle (android.os.Bundle)2 Handler (android.os.Handler)2 Message (android.os.Message)2 LayoutInflater (android.view.LayoutInflater)2 CompoundButton (android.widget.CompoundButton)2 ImageView (android.widget.ImageView)2 ListView (android.widget.ListView)2 AlertDialog (androidx.appcompat.app.AlertDialog)2 JsonArray (com.google.gson.JsonArray)2 JsonParser (com.google.gson.JsonParser)2