You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
9.6 KiB

package com.likesoft.mdedal;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.provider.Settings;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
//import com.likesoft.mdedaltest.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static com.likesoft.mdedal.ListActivity.LOG;
public class PreferActivity extends Activity {
private static final int REQ_STORAGE_PERMISSION = 1001;
private static final int REQ_MANAGE_ALL_FILES = 2296;
// EditText hostName;
EditText user;
EditText password;
EditText newpassword;
// CheckBox tablet;
// Spinner refresh;
//TextView version;
TextView textViewHostUrl;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prefer);
// hostName = (EditText) findViewById(R.id.editTextHost);
user = (EditText) findViewById(R.id.editTextUser);
password = (EditText) findViewById(R.id.editTextPassword);
newpassword = (EditText) findViewById(R.id.editTextNewPassword);
// tablet = (CheckBox) findViewById(R.id.checkBoxTablet);
// refresh = (Spinner) findViewById(R.id.spinner_refresh);
//version = (TextView) findViewById(R.id.textViewVersion);
//textViewHostUrl
textViewHostUrl = (TextView) findViewById(R.id.textViewHostUrl);
PackageInfo pinfo;
try {
pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
//versionNumber = pinfo.versionCode;
String versionName = pinfo.versionName;
//version.setText("Wersja aplikacji: "+versionName);
setTitle("Logowanie - Wersja aplikacji: "+versionName);
} catch (PackageManager.NameNotFoundException e2) {
Log.e(LOG, "", e2);
}
Context context = getApplicationContext();
prefs = PreferenceManager.getDefaultSharedPreferences(context);
updateUI();
//Log.d("my_debug", "oncreate prefs");
Button ok = (Button)findViewById(R.id.buttonSave);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Zanim zapiszemy preferencje, upewnijmy się, że mamy dostęp do pamięci
if (!hasStorageAccess()) {
requestStorageAccess();
Toast.makeText(PreferActivity.this, "Brak uprawnień do zapisu w pamięci. Przyznaj uprawnienie i spróbuj ponownie.", Toast.LENGTH_LONG).show();
return;
}
if( newpassword.getText().toString().length()>0){
savePreferences();
new Thread( new Runnable() {
@Override
public void run() {
DedalHttpConnection con = new DedalHttpConnection(getApplicationContext());
Map<String, String> params_post = new HashMap<String, String>();
params_post.put("oldpass", password.getText().toString());
params_post.put("pass1", newpassword.getText().toString());
params_post.put("pass2", newpassword.getText().toString());
String response = "";
try{
response = con.sendPost("public/mobi.php?page=login_change", params_post);
}catch (Exception e){
Log.e( "BLAD ", "Bląd połączenia");
}
if( response.contains("zmienione") ) {
//jeśli się powiodło:
// password.setText(newpassword.getText());
Editor e = prefs.edit();
e.putString("password", DedalHelper.encodeB64(newpassword.getText().toString()));
e.commit();
Log.d("PASSWORD", "hasło zostało zmienione na: "+ newpassword.getText().toString());
//savePreferences();
Button ok = (Button)findViewById(R.id.buttonSave);
ok.post(new Runnable() {
@Override
public void run() {
Toast.makeText(PreferActivity.this, "Hasło zmienione.", Toast.LENGTH_LONG);
PreferActivity.this.setResult(RESULT_OK);
finish();
}
});
}
else {
Log.d("PASSWORD", "problem ze zmianą hasła, response: " + response);
Toast.makeText(PreferActivity.this, "Problem ze zmianą hasła", Toast.LENGTH_LONG);
}
}
}).start();
}else {
savePreferences();
PreferActivity.this.setResult(RESULT_OK);
finish();
}
}
});
}
private boolean hasStorageAccess() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int write = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int read = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
return write == PackageManager.PERMISSION_GRANTED && read == PackageManager.PERMISSION_GRANTED;
}
}
private void requestStorageAccess() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQ_MANAGE_ALL_FILES);
} catch (Exception ex) {
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, REQ_MANAGE_ALL_FILES);
}
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
REQ_STORAGE_PERMISSION);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_MANAGE_ALL_FILES) {
if (hasStorageAccess()) {
Toast.makeText(this, "Przyznano dostęp do plików.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Dostęp do plików nadal zablokowany.", Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQ_STORAGE_PERMISSION) {
boolean granted = true;
if (grantResults == null || grantResults.length == 0) granted = false;
for (int r : grantResults) {
if (r != PackageManager.PERMISSION_GRANTED) { granted = false; break; }
}
if (granted) {
Toast.makeText(this, "Uprawnienia do pamięci przyznane.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Odmówiono uprawnień do pamięci.", Toast.LENGTH_LONG).show();
}
}
}
public static void deleteFiles(String path) {
File file = new File(path);
if (file.exists()) {
String deleteCmd = "rm -r " + path;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) { }
}
}
void savePreferences() {
// TODO Auto-generated method stub
String currentHost = prefs.getString("hostName", "https://dedal.eos-ksi.pl");
Editor e = prefs.edit();
e.putString("hostName", currentHost);
// e.putBoolean("tablet", tablet.isChecked());
String login = user.getText().toString().trim();
if( login.contains("@")){
String loginTable[] = login.split("@");
e.putString("user", loginTable[0]);
if("reset".equals(loginTable[1]) ){
e.putString("certSerialNumber", "");
deleteFiles(Environment.getExternalStorageDirectory()+ "/dedal/");
}else {
if( loginTable[1].startsWith("http")) {
e.putString("hostName", loginTable[1]);
}
}
login = loginTable[0];
}else {
e.putString("user", login);
}
e.putString("password", DedalHelper.encodeB64(password.getText().toString()));
// e.putInt("refresh", refresh.getSelectedItemPosition());
String dirPath = Environment.getExternalStorageDirectory()+ "/dedal/"+ login;
e.putString("dir", dirPath);
e.putBoolean("tablet", true);
e.putInt("refresh", 2);
e.commit();
// Utwórz folder użytkownika jeśli nie istnieje
try {
File dir = new File(dirPath);
if (!dir.exists()) {
boolean ok = dir.mkdirs();
if (!ok) {
Toast.makeText(this, "Nie można utworzyć katalogu:" + dirPath, Toast.LENGTH_LONG).show();
}
}
} catch (Throwable t) {
Log.e(LOG, "Błąd tworzenia katalogu: " + t.getMessage(), t);
}
//usunięcie starej aplikacji:
deleteFiles(Environment.getExternalStorageDirectory()+ "/likesoft");
deleteFiles(Environment.getExternalStorageDirectory()+ "/likesofttest");
File fLockLogin = new File(prefs.getString("dir", "") + "/dedalsrv.wrongLogin");
if( fLockLogin.exists()){
fLockLogin.delete();
}
}
private void updateUI() {
// tablet.setChecked(prefs.getBoolean("tablet", false));
// hostName.setText(prefs.getString("hostName", "http://likesoft/dedal/develop/psql"));
// hostName.setText(prefs.getString("hostName", "https://dedal.eos-ksi.pl"));
user.setText(prefs.getString("user", ""));
password.setText("");
textViewHostUrl.setText(prefs.getString("hostName", "https://dedal.eos-ksi.pl"));
//password.setText(prefs.getString("password", ""));
// refresh.setSelection(prefs.getInt("refresh", 0));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.prefer, menu);
return true;
}
}