Skip to content

Commit

Permalink
code and resource optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Jul 10, 2020
1 parent b29aed3 commit 44c1ae9
Show file tree
Hide file tree
Showing 30 changed files with 151 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.mikepenz.aboutlibraries.LibsBuilder;
Expand All @@ -15,7 +16,7 @@
public class Credits extends AppCompatActivity {

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

View aboutPage = new AboutPage(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thegreek.niko.logviewer_for_openHAB;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
Expand All @@ -18,15 +19,19 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialogFragment;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

public class EndUserConsent extends AppCompatDialogFragment {

@NotNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
SharedPreferences mySPR = getActivity().getSharedPreferences("Speicherstand", 0);
SharedPreferences mySPR = Objects.requireNonNull(getActivity()).getSharedPreferences("Safe", 0);
final SharedPreferences.Editor editor = mySPR.edit();

SpannableString ss = new SpannableString(getString(R.string.end_user_consent_message));
Expand Down Expand Up @@ -70,17 +75,17 @@ public void onClick(@NonNull View view) {
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.firebaseAnalytics.setAnalyticsCollectionEnabled(true);
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sDF = new SimpleDateFormat("dd.MM.yyyy");
SimpleDateFormat sDF2 = new SimpleDateFormat("HH:mm:ss");
editor.putString("datum", sDF.format(date));
editor.putString("uhrzeit", sDF2.format(date));
@SuppressLint("SimpleDateFormat") SimpleDateFormat sDF = new SimpleDateFormat("dd.MM.yyyy");
@SuppressLint("SimpleDateFormat") SimpleDateFormat sDF2 = new SimpleDateFormat("HH:mm:ss");
editor.putString("date", sDF.format(date));
editor.putString("time", sDF2.format(date));
editor.putBoolean("firstStart", false).apply();
}
})
.setNegativeButton(getString(R.string.end_user_consent_button_2), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
System.exit(0);
Objects.requireNonNull(getActivity()).finish();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialogFragment;

import java.util.Objects;

public class EndUserConsent2 extends AppCompatDialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
SharedPreferences mySPR = getActivity().getSharedPreferences("Speicherstand", 0);
SharedPreferences mySPR = Objects.requireNonNull(getActivity()).getSharedPreferences("Safe", 0);
final SharedPreferences.Editor editor = mySPR.edit();
editor.apply();

String message = getString(R.string.end_user_consent2_message_1);

message += mySPR.getString("datum", "") + getString(R.string.end_user_consent2_message_2) + mySPR.getString("uhrzeit", "");
message += mySPR.getString("date", "") + getString(R.string.end_user_consent2_message_2) + mySPR.getString("time", "");

SpannableString spannableString = new SpannableString(message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_credits);

TextView textView5 = findViewById(R.id.credits_icons_1);
TextView textView6 = findViewById(R.id.credits_icons_2);
TextView textView7 = findViewById(R.id.credits_icons_3);
TextView textView8 = findViewById(R.id.credits_icons_4);
TextView textView9 = findViewById(R.id.credits_icons_5);
TextView textView10 = findViewById(R.id.credits_icons_6);
TextView textView11 = findViewById(R.id.credits_icons_7);
TextView textView12 = findViewById(R.id.credits_icons_8);
TextView textView13 = findViewById(R.id.credits_icons_9);

setSpans(textView5, getString(R.string.credits_icons_1), "https://www.flaticon.com/authors/dave-gandy", 24, 34);
setSpans(textView6, getString(R.string.credits_icons_2), "https://www.flaticon.com/authors/smartline", 21, 30);
setSpans(textView7, getString(R.string.credits_icons_3), "https://www.flaticon.com/authors/those-icons", 20, 31);
setSpans(textView8, getString(R.string.credits_icons_4), "https://www.flaticon.com/authors/those-icons", 20, 31);
setSpans(textView9, getString(R.string.credits_icons_5), "https://www.flaticon.com/authors/lyolya", 26, 32);
setSpans(textView10, getString(R.string.credits_icons_6), "https://www.flaticon.com/authors/those-icons", 24, 35);
setSpans(textView11, getString(R.string.credits_icons_7), "https://www.flaticon.com/authors/freepik", 27, 34);
setSpans(textView12, getString(R.string.credits_icons_8), "https://www.flaticon.com/authors/freepik", 20, 27);
setSpans(textView13, getString(R.string.credits_icons_9), "https://www.flaticon.com/authors/freepik", 24, 31);
TextView[] textViews = new TextView[9];

for (int i = 1; i <= 9; i++) {
textViews[i-1] = findViewById(getResources().getIdentifier("credits_icons_" + i, "id", getPackageName()));
}

setSpans(textViews[0], getString(R.string.credits_icons_1), getString(R.string.credits_icons_links_5), 24, 34);
setSpans(textViews[1], getString(R.string.credits_icons_2), getString(R.string.credits_icons_links_4), 21, 30);
setSpans(textViews[2], getString(R.string.credits_icons_3), getString(R.string.credits_icons_links_2), 20, 31);
setSpans(textViews[3], getString(R.string.credits_icons_4), getString(R.string.credits_icons_links_2), 20, 31);
setSpans(textViews[4], getString(R.string.credits_icons_5), getString(R.string.credits_icons_links_3), 26, 32);
setSpans(textViews[5], getString(R.string.credits_icons_6), getString(R.string.credits_icons_links_2), 24, 35);
setSpans(textViews[6], getString(R.string.credits_icons_7), getString(R.string.credits_icons_links_1), 27, 34);
setSpans(textViews[7], getString(R.string.credits_icons_8), getString(R.string.credits_icons_links_1), 20, 27);
setSpans(textViews[8], getString(R.string.credits_icons_9), getString(R.string.credits_icons_links_1), 24, 31);
}

void setSpans(TextView textView, String string, final String link, int startChar, int endChar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

firebaseAnalytics = FirebaseAnalytics.getInstance(this);
mySPR = this.getSharedPreferences("Speicherstand", 0);
mySPR = this.getSharedPreferences("Safe", 0);
editor = mySPR.edit();
editor.apply();
Toasty.Config.getInstance().allowQueue(false).apply();

getSupportFragmentManager().beginTransaction().replace(R.id.start, new MainFragment()).commit();

if (mySPR.getBoolean("firstStart", true) || mySPR.getString("datum", "").equals("")) {
if (mySPR.getBoolean("firstStart", true) || mySPR.getString("date", "").equals("")) {
EndUserConsent endUserConsent = new EndUserConsent();
endUserConsent.setCancelable(false);
endUserConsent.show(getSupportFragmentManager(), "Endnutzer-Einwilligung");
Expand All @@ -59,33 +59,27 @@ public void onResponse(String response) {
int neuesterVersionCode = Integer.parseInt(parts2[0]);
parts = parts2[1].split("\"");
parts2 = parts[1].split("\"");
editor.putString("neuesteVersion", parts2[0]);
editor.putString("newestVersion", parts2[0]);
editor.apply();

if (BuildConfig.VERSION_CODE != neuesterVersionCode) {
System.out.println("----------\nUpdate verfügbar: " + mySPR.getString("neuesteVersion", "") + "\n----------");

UpdateDialog updateDialog = new UpdateDialog();
updateDialog.setCancelable(false);
updateDialog.show(getSupportFragmentManager(), "Update-Dialog");

ActivityCompat.requestPermissions(activity , new String[]{Manifest.permission.INTERNET, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.REQUEST_INSTALL_PACKAGES}, 1);
} else {
System.out.println("----------\nApp auf dem neuesten Stand\n----------");
}
}

@Override
public void onError(ANError anError) {
System.out.println("----------\nUpdate-Abfrage fehlgeschlagen: " + anError.getErrorBody() + "\n----------");
}
});
}

@Override
public void onBackPressed() {
System.exit(0);
finish();
}

static void changeOrientation(Activity activity, int orientation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
TextView versionView = v.findViewById(R.id.version_view);
ImageView settings = v.findViewById(R.id.imageView);

mySPR = v.getContext().getSharedPreferences("Speicherstand", 0);
mySPR = v.getContext().getSharedPreferences("Safe", 0);
editor = mySPR.edit();
editor.apply();

statusWiederherstellung();
versionView.setText(BuildConfig.VERSION_NAME);
Expand Down Expand Up @@ -114,13 +115,15 @@ public void onClick(View view) {

connectButton.setText(getString(R.string.connect_button_2));
} else {
assert getFragmentManager() != null;
getFragmentManager().beginTransaction()
.replace(R.id.start, new WebViewFragment())
.addToBackStack(null)
.commit();

if (view != null) {
InputMethodManager imm = (InputMethodManager) view.getContext().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Expand Down Expand Up @@ -194,6 +197,7 @@ private void statusWiederherstellung() {
connectCheck.setChecked(mySPR.getBoolean("connectCheck", false));

if (mySPR.getBoolean("autoStart", false) && connectCheck.isChecked()) {
assert getFragmentManager() != null;
getFragmentManager().beginTransaction()
.replace(R.id.start, new WebViewFragment())
.addToBackStack(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

Expand All @@ -16,6 +17,13 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_privacy_policy);

TextView[] textViews = new TextView[21];

for (int i = 1; i <= 21; i++) {
textViews[i-1] = findViewById(getResources().getIdentifier("textView" + i, "id", getPackageName()));
textViews[i-1].setText(getResources().getStringArray(R.array.privacy_policy)[i-1]);
}

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

Expand All @@ -16,6 +17,13 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_terms_of_use);

TextView[] textViews = new TextView[7];

for (int i = 1; i <= 7; i++) {
textViews[i-1] = findViewById(getResources().getIdentifier("textView" + i, "id", getPackageName()));
textViews[i-1].setText(getResources().getStringArray(R.array.terms_of_use)[i-1]);
}

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.core.content.ContextCompat;

import java.util.Objects;

import es.dmoral.toasty.Toasty;

import static android.content.Context.DOWNLOAD_SERVICE;
Expand All @@ -34,7 +36,7 @@ public class UpdateDialog extends AppCompatDialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final SharedPreferences mySPR = getActivity().getSharedPreferences("Speicherstand", 0);
final SharedPreferences mySPR = Objects.requireNonNull(getActivity()).getSharedPreferences("Safe", 0);
final SharedPreferences.Editor editor = mySPR.edit();
editor.apply();

Expand All @@ -57,8 +59,8 @@ public void onClick(@NonNull View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/nikothegreek/logviewer-for-openhab-app/releases/latest")));
}
};
String message = String.format(getString(R.string.update_dialog_message), mySPR.getString("neuesteVersion", ""), BuildConfig.VERSION_NAME);
String index = "changelog";
String message = String.format(getString(R.string.update_dialog_message), mySPR.getString("newestVersion", ""), BuildConfig.VERSION_NAME);
String index = getString(R.string.update_dialog_changelog);
SpannableString spannableString = new SpannableString(message);
spannableString.setSpan(clickableSpan, message.indexOf(index), message.indexOf(index) + index.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageView.setText(spannableString);
Expand All @@ -70,14 +72,15 @@ public void onClick(@NonNull View view) {
.setPositiveButton(getString(R.string.update_dialog_button_1), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
String link = "https://github.com/nikothegreek/logviewer-for-openhab-app/releases/download/v" + mySPR.getString("neuesteVersion", "") + "/LogViewerforopenHAB_" +
mySPR.getString("neuesteVersion", "") + ".apk";
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(getContext()), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
String link = "https://github.com/nikothegreek/logviewer-for-openhab-app/releases/download/v" + mySPR.getString("newestVersion", "") + "/LogViewerforopenHAB_" +
mySPR.getString("newestVersion", "") + ".apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link))
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(link, null, null))
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
DownloadManager downloadManager = (DownloadManager) Objects.requireNonNull(getActivity()).getSystemService(DOWNLOAD_SERVICE);
assert downloadManager != null;
downloadManager.enqueue(request);
} else {
Toasty.error(getContext(), getString(R.string.update_dialog_error), Toasty.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thegreek.niko.logviewer_for_openHAB;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
Expand All @@ -23,6 +24,8 @@

import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar;

import java.util.Objects;

import es.dmoral.toasty.Toasty;

public class WebViewFragment extends Fragment {
Expand All @@ -32,6 +35,7 @@ public class WebViewFragment extends Fragment {

private boolean viewLocked = true;

@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -42,7 +46,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
final FloatingActionButton backButton = v.findViewById(R.id.back_utton);
webView = v.findViewById(R.id.webview);

final SharedPreferences mySPR = this.getActivity().getSharedPreferences("Speicherstand", 0);
final SharedPreferences mySPR = Objects.requireNonNull(this.getActivity()).getSharedPreferences("Safe", 0);
editor = mySPR.edit();

final WebSettings webSettings = webView.getSettings();
Expand Down Expand Up @@ -129,6 +133,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
public void onClick(View view) {
final boolean autoStartTemp = mySPR.getBoolean("autoStart", false);
editor.putBoolean("autoStart", false).apply();
assert getFragmentManager() != null;
getFragmentManager().beginTransaction().replace(R.id.start, new MainFragment()).commit();

final Handler handler2 = new Handler();
Expand Down Expand Up @@ -189,6 +194,7 @@ public void onSequenceCanceled(TapTarget lastTarget) {
return v;
}

@SuppressLint("ClickableViewAccessibility")
private void setTouchable(boolean b) {
if (b) {
webView.setOnTouchListener(new View.OnTouchListener() {
Expand Down
Binary file added app/src/main/res/drawable-v24/icon_arrow_left.webp
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added app/src/main/res/drawable-v24/icon_font.webp
Binary file not shown.
Binary file removed app/src/main/res/drawable-v24/icon_libraries.png
Binary file not shown.
Binary file added app/src/main/res/drawable-v24/icon_libraries.webp
Binary file not shown.
Binary file removed app/src/main/res/drawable-v24/icon_lock.png
Binary file not shown.
Binary file added app/src/main/res/drawable-v24/icon_lock.webp
Binary file not shown.
Binary file removed app/src/main/res/drawable-v24/icon_lock_2.png
Binary file not shown.
Binary file added app/src/main/res/drawable-v24/icon_lock_2.webp
Binary file not shown.
Binary file removed app/src/main/res/drawable-v24/icon_question.png
Binary file not shown.
Binary file added app/src/main/res/drawable-v24/icon_question.webp
Binary file not shown.
Binary file removed app/src/main/res/drawable/icon_arrow_left.png
Binary file not shown.
Binary file removed app/src/main/res/drawable/icon_font.png
Binary file not shown.
2 changes: 1 addition & 1 deletion app/src/main/res/layout-port/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:text="@string/credits"
android:text="@string/about"
android:textColor="#FF6600"
android:textSize="16sp"
android:textStyle="bold"
Expand Down
Loading

0 comments on commit 44c1ae9

Please sign in to comment.