Skip to content

Commit

Permalink
Merge pull request #80 from SUPLA/develop
Browse files Browse the repository at this point in the history
v2.3.12
  • Loading branch information
przemyslawzygmunt authored Jul 3, 2019
2 parents 054a4eb + c4eefea commit 47e0d93
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.supla.android"
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
versionCode 65
versionName "2.3.11"
versionCode 66
versionName "2.3.12"

sourceSets.main {
jniLibs.srcDir 'src/main/libs'
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/org/supla/android/AddWizardActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ of the License, or (at your option) any later version.
import android.widget.Spinner;
import android.widget.TextView;

import org.supla.android.lib.Preferences;
import org.supla.android.lib.SuplaRegisterError;
import org.supla.android.lib.SuplaRegistrationEnabled;

Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/org/supla/android/CfgActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ of the License, or (at your option) any later version.
*/

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
Expand All @@ -35,7 +33,6 @@ of the License, or (at your option) any later version.
import android.widget.TextView;

import org.supla.android.db.DbHelper;
import org.supla.android.lib.Preferences;

public class CfgActivity extends NavigationActivity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ of the License, or (at your option) any later version.
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.github.mikephil.charting.charts.BarChart;

import com.github.mikephil.charting.charts.CombinedChart;

import org.supla.android.charts.ThermostatChartHelper;
import org.supla.android.db.Channel;
import org.supla.android.db.ChannelBase;
import org.supla.android.db.ChannelExtendedValue;
import org.supla.android.lib.Preferences;
import org.supla.android.lib.SuplaConst;
import org.supla.android.listview.ChannelListView;
import org.supla.android.listview.DetailLayout;
Expand Down
123 changes: 123 additions & 0 deletions app/src/main/java/org/supla/android/Encryption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.supla.android;
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Encryption {
private static SecretKey generateKey(String password)
throws NoSuchAlgorithmException, InvalidKeySpecException
{
password = password.substring(0, 32);
String alignment = "";

for(int a=0;a<32-password.length();a++) {
alignment+="0";
}

password+=alignment;

return new SecretKeySpec(password.getBytes(), "AES");
}

public static byte[] encryptData(byte[] data, String password)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, InvalidKeySpecException {
/* Encrypt the message. */
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, generateKey(password));
byte[] cipherText = cipher.doFinal(data);
return cipherText;
}

public static byte[] decryptData(byte[] cipherText, String password)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeySpecException {
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, generateKey(password));
return cipher.doFinal(cipherText);
}

public static byte[] decryptDataWithNullOnException(byte[] cipherText, String password) {
byte[] result = null;
try {
result = decryptData(cipherText, password);
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidParameterSpecException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}

return result;
}

public static byte[] encryptDataWithNullOnException(byte[] data, String password) {
byte[] result = null;
try {
result = encryptData(data, password);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidParameterSpecException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}

return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.supla.android.lib;
package org.supla.android;

/*
Copyright (C) AC SOFTWARE SP. Z O.O.
Expand All @@ -18,13 +18,15 @@ of the License, or (at your option) any later version.
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

import android.os.Build;
import android.preference.PreferenceManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Base64;

import org.supla.android.SuplaApp;

import org.supla.android.lib.SuplaClient;
import org.supla.android.lib.SuplaConst;
import java.util.Random;

public class Preferences {
Expand All @@ -45,15 +47,19 @@ public class Preferences {
private static final String pref_hp_eco_reduction = "pref_hp_eco_reduction";

private SharedPreferences _prefs;
private Context _context;

public Preferences(Context context) {
_prefs = PreferenceManager.getDefaultSharedPreferences(context);
_context = context;

if ( getCfgVersion() == 0 ) {

setAdvancedCfg(!getServerAddress().isEmpty() && getAccessID() != 0 && !getAccessIDpwd().isEmpty());
setCfgVersion(2);
}

context.getContentResolver();
}

private int getCfgVersion() {
Expand All @@ -66,11 +72,51 @@ public void setCfgVersion(int version) {
editor.apply();
}

private String getDeviceID() {
String Id = null;

try {
final TelephonyManager tm = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Id = Settings.Secure.getString(_context.getContentResolver(),
Settings.Secure.ANDROID_ID);
} else {
Id = Build.SERIAL;
}

Id += "-" + Build.BOARD+
"-"+Build.BRAND+
"-"+Build.DEVICE+
"-"+Build.HARDWARE;
} catch (Exception e) {
e.printStackTrace();
}

return (Id == null || Id.length() == 0) ? "unknown" : Id;
}

private void encryptAndSave(String pref_key, byte[] data) {
SharedPreferences.Editor editor = _prefs.edit();
editor.putString(pref_key,
Base64.encodeToString(
Encryption.encryptDataWithNullOnException(
data, getDeviceID()), Base64.DEFAULT));
editor.putBoolean(pref_key+"_encrypted", true);
editor.apply();
}

private byte[] getRandom(String pref_key, int size) {

byte[] result = Base64.decode(_prefs.getString(pref_key, ""), Base64.DEFAULT);

if ( result.length != size ) {
if ( !_prefs.getBoolean(pref_key+"_encrypted", false) ) {
encryptAndSave(pref_key, result);
} else {
result = Encryption.decryptDataWithNullOnException(result, getDeviceID());
}

if ( result == null || result.length != size ) {

Random random = new Random();
result = new byte[size];
Expand All @@ -79,10 +125,7 @@ private byte[] getRandom(String pref_key, int size) {
result[a] = (byte)random.nextInt(255);
}

SharedPreferences.Editor editor = _prefs.edit();
editor.putString(pref_key, Base64.encodeToString(result, Base64.DEFAULT));
editor.apply();

encryptAndSave(pref_key, result);
}

return result;
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/org/supla/android/StartActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ of the License, or (at your option) any later version.
import android.view.Display;
import android.widget.TextView;

import org.supla.android.lib.Preferences;

public class StartActivity extends Activity {


Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/supla/android/lib/SuplaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ of the License, or (at your option) any later version.
import org.json.JSONObject;
import org.json.JSONTokener;
import org.supla.android.BuildConfig;
import org.supla.android.Preferences;
import org.supla.android.SuplaApp;
import org.supla.android.Trace;
import org.supla.android.db.DbHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ of the License, or (at your option) any later version.

import android.content.Context;

import org.supla.android.Preferences;
import org.supla.android.R;
import org.supla.android.lib.SuplaConst;

public class SuplaRegisterError {

Expand Down

0 comments on commit 47e0d93

Please sign in to comment.