Compare commits

...

5 Commits

10 changed files with 246 additions and 93 deletions

View File

@ -11,8 +11,8 @@ android {
compileSdkVersion 29 compileSdkVersion 29
buildToolsVersion '29.0.2' buildToolsVersion '29.0.2'
useLibrary 'org.apache.http.legacy' useLibrary 'org.apache.http.legacy'
versionCode 98 versionCode 99
versionName "1.6.22" versionName "1.6.23"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -418,7 +418,34 @@ public class ActivityMainScreen extends ActivityGeneric
Miscellaneous.logEvent("i", "ActivityMainScreen", "Activity not running. No need to update.", 5); Miscellaneous.logEvent("i", "ActivityMainScreen", "Activity not running. No need to update.", 5);
if(activityMainScreenInstance != null) if(activityMainScreenInstance != null)
activityMainScreenInstance.checkForNews(); {
if(!Settings.hasBeenDone(Settings.constNewsOptInDone))
newsOptIn();
else
activityMainScreenInstance.checkForNews();
Settings.considerDone(Settings.constNewsOptInDone);
Settings.writeSettings(Miscellaneous.getAnyContext());
}
}
static void newsOptIn()
{
AlertDialog.Builder builder = new AlertDialog.Builder(Miscellaneous.getAnyContext());
builder.setMessage(Miscellaneous.getAnyContext().getResources().getString(R.string.newsOptIn));
builder.setPositiveButton(Miscellaneous.getAnyContext().getResources().getString(R.string.yes), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Settings.displayNewsOnMainScreen = true;
Settings.writeSettings(Miscellaneous.getAnyContext());
activityMainScreenInstance.checkForNews();
}
});
builder.setNegativeButton(Miscellaneous.getAnyContext().getResources().getString(R.string.no), null);
builder.create().show();
} }
@Override @Override
@ -572,57 +599,31 @@ public class ActivityMainScreen extends ActivityGeneric
synchronized void checkForNews() synchronized void checkForNews()
{ {
News.AsyncTaskDownloadNews dnTask = new News.AsyncTaskDownloadNews(); if(Settings.displayNewsOnMainScreen)
dnTask.execute(ActivityMainScreen.this); {
News.AsyncTaskDownloadNews dnTask = new News.AsyncTaskDownloadNews();
// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); dnTask.execute(ActivityMainScreen.this);
// StrictMode.setThreadPolicy(policy); }
// Calendar now = Calendar.getInstance();
// if (true || Settings.lastNewsPolltime == -1 || now.getTimeInMillis() >= Settings.lastNewsPolltime + (long)(Settings.pollNewsEveryXDays * 24 * 60 * 60 * 1000))
// {
// ArrayList<News> newsToDisplay;
//
//// if(Settings.lastNewsPolltime == -1)
// newsToDisplay = News.downloadNews(ActivityMainScreen.this);
//// else
//// newsToDisplay = News.downloadNews(ActivityMainScreen.this, Miscellaneous.calendarFromLong(Settings.lastNewsPolltime));
//
// if (newsToDisplay.size() > 0)
// {
// activityMainScreenInstance.tvMainScreenNote3.setText(newsToDisplay.get(0).toString());
// activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.VISIBLE);
// }
// else
// {
// activityMainScreenInstance.tvMainScreenNote3.setText("");
// activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.GONE);
// }
// }
} }
public void processNewsResult(ArrayList<News> newsToDisplay) public void processNewsResult(ArrayList<News> newsToDisplay)
{ {
if(Settings.displayNewsOnMainScreen) try
{ {
try if (newsToDisplay.size() > 0)
{ {
if (newsToDisplay.size() > 0) activityMainScreenInstance.tvMainScreenNote3.setText(HtmlCompat.fromHtml(newsToDisplay.get(0).toStringHtml(), 0));
{ activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.VISIBLE);
activityMainScreenInstance.tvMainScreenNote3.setText(HtmlCompat.fromHtml(newsToDisplay.get(0).toStringHtml(), 0));
activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.VISIBLE);
}
else
{
activityMainScreenInstance.tvMainScreenNote3.setText("");
activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.GONE);
}
} }
catch(Exception e) else
{ {
Miscellaneous.logEvent("e", "Error displaying news", Log.getStackTraceString(e), 3); activityMainScreenInstance.tvMainScreenNote3.setText("");
activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.GONE);
} }
} }
catch(Exception e)
{
Miscellaneous.logEvent("e", "Error displaying news", Log.getStackTraceString(e), 3);
}
} }
} }

View File

@ -977,7 +977,7 @@ public class ActivityPermissions extends Activity
and simply disable features while keeping the notification alive. The user may dismiss it anyway. and simply disable features while keeping the notification alive. The user may dismiss it anyway.
*/ */
Miscellaneous.logEvent("w", "Denied permissions", getResources().getString(R.string.theFollowingPermissionsHaveBeenDenied) + Miscellaneous.explode(deniedPermissions), 3); Miscellaneous.logEvent("w", "Denied permissions", getResources().getString(R.string.theFollowingPermissionsHaveBeenDenied) + Miscellaneous.explode(", ", deniedPermissions), 3);
// this.finish(); // this.finish();
} }
else else

View File

@ -21,9 +21,6 @@ import android.provider.MediaStore;
import android.provider.Settings.Secure; import android.provider.Settings.Secure;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
import com.jens.automation2.location.LocationProvider; import com.jens.automation2.location.LocationProvider;
import com.jens.automation2.receivers.PhoneStatusListener; import com.jens.automation2.receivers.PhoneStatusListener;
@ -47,11 +44,14 @@ import org.xml.sax.SAXException;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader; import java.io.StringReader;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -81,6 +81,8 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import androidx.core.app.NotificationCompat;
import static com.jens.automation2.AutomationService.NOTIFICATION_CHANNEL_ID; import static com.jens.automation2.AutomationService.NOTIFICATION_CHANNEL_ID;
import static com.jens.automation2.AutomationService.channelName; import static com.jens.automation2.AutomationService.channelName;
@ -313,55 +315,74 @@ public class Miscellaneous extends Service
{ {
if(writeableFolderStringCache == null) if(writeableFolderStringCache == null)
{ {
if(!ActivityPermissions.havePermission(ActivityPermissions.writeExternalStoragePermissionName, Miscellaneous.getAnyContext())) // Use the app-specific folder as new default.
{ writeableFolderStringCache = Miscellaneous.getAnyContext().getFilesDir().getAbsolutePath();
// Use the app-specific folder as new default.
writeableFolderStringCache = Miscellaneous.getAnyContext().getFilesDir().getAbsolutePath(); File newConfigFile = new File(writeableFolderStringCache + "/" + XmlFileInterface.settingsFileName);
return writeableFolderStringCache;
}
else
{
//TODO: We have the storage permission, probably because it's an old installation. Files should be migrated to app-specific folder.
String testPath = null;
File folder = null;
try migration:
if (!newConfigFile.exists())
{
if (ActivityPermissions.havePermission(ActivityPermissions.writeExternalStoragePermissionName, Miscellaneous.getAnyContext()))
{ {
String[] foldersToTestArray = new String[] // We have the storage permission, probably because it's an old installation. Files should be migrated to app-specific folder.
{
Environment.getExternalStorageDirectory().getAbsolutePath(),
"/storage/emulated/0",
"/HWUserData",
"/mnt/sdcard"
};
for (String f : foldersToTestArray) String testPath = null;
File folder = null;
try
{ {
if (testFolder(f)) String[] foldersToTestArray = new String[]
{
Environment.getExternalStorageDirectory().getAbsolutePath(),
"/storage/emulated/0",
"/HWUserData",
"/mnt/sdcard"
};
for (String f : foldersToTestArray)
{ {
String pathToUse = f + "/" + Settings.folderName; // if (testFolder(f))
Miscellaneous.logEvent("i", "Path", "Using " + pathToUse + " to store settings and log.", 2); // {
String pathToUse = f + "/" + Settings.folderName;
// Toast.makeText(getAnyContext(), "Using " + pathToUse + " to store settings and log.", Toast.LENGTH_LONG).show(); // Toast.makeText(getAnyContext(), "Using " + pathToUse + " to store settings and log.", Toast.LENGTH_LONG).show();
return pathToUse; // Migrate existing files
File oldDirectory = new File(pathToUse);
File newDirectory = new File(writeableFolderStringCache);
File oldConfigFilePath = new File(pathToUse + "/" + XmlFileInterface.settingsFileName);
if (oldConfigFilePath.exists() && oldConfigFilePath.canWrite())
{
Miscellaneous.logEvent("i", "Path", "Found old path " + pathToUse + " for settings and logs. Migrating old files to new directory.", 2);
for (File fileToBeMoved : oldDirectory.listFiles())
{
File dstFile = new File(writeableFolderStringCache + "/" + fileToBeMoved.getName());
/*
For some stupid reason Android's file.moveTo can't move files between
mount points. That's why we have to copy it and delete the src if successful.
*/
if(copyFileUsingStream(fileToBeMoved, dstFile))
fileToBeMoved.delete();
}
String message = String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.filesHaveBeenMovedTo), newDirectory.getAbsolutePath());
Miscellaneous.writeStringToFile(oldDirectory.getAbsolutePath() + "/readme.txt", message);
break migration;
}
// }
} }
else } catch (Exception e)
Miscellaneous.logEvent("e", "getWritableFolder", folder.getAbsolutePath() + " does not exist and could not be created.", 3); {
Log.w("getWritableFolder", folder + " not writable.");
} }
} catch (Exception e)
{
Log.w("getWritableFolder", folder + " not writable.");
} }
// do not change to logEvent() - we can't write
Toast.makeText(getAnyContext(), "No writable folder could be found.", Toast.LENGTH_LONG).show();
Log.e("getWritableFolder", "No writable folder could be found.");
return null;
} }
} }
else
return writeableFolderStringCache; return writeableFolderStringCache;
} }
protected final static String logFileName = "Automation_logfile.txt"; protected final static String logFileName = "Automation_logfile.txt";
@ -843,13 +864,21 @@ public class Miscellaneous extends Service
return builder; return builder;
} }
public static String explode(ArrayList<String> arrayList) public static String explode(String glue, ArrayList<String> arrayList)
{ {
StringBuilder builder = new StringBuilder(); if(arrayList != null)
for(String s : arrayList) {
builder.append(s); StringBuilder builder = new StringBuilder();
for (String s : arrayList)
builder.append(s + glue);
return builder.toString(); if (builder.length() > 0)
builder.delete(builder.length() - glue.length(), builder.length());
return builder.toString();
}
else
return "";
} }
public static boolean isGooglePlayInstalled(Context context) public static boolean isGooglePlayInstalled(Context context)
@ -1047,4 +1076,32 @@ public class Miscellaneous extends Service
return null; return null;
} }
} }
public static boolean copyFileUsingStream(File source, File dest) throws IOException
{
boolean returnValue = false;
InputStream is = null;
OutputStream os = null;
try
{
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
{
os.write(buffer, 0, length);
}
returnValue = true;
}
finally
{
is.close();
os.close();
}
return returnValue;
}
} }

View File

@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -16,6 +17,8 @@ public class Settings implements SharedPreferences
public static final int newsDisplayForXDays = 3; public static final int newsDisplayForXDays = 3;
public static final String folderName = "Automation"; public static final String folderName = "Automation";
public static final String constNewsOptInDone ="newsOptInDone";
public static long minimumDistanceChangeForGpsUpdate; public static long minimumDistanceChangeForGpsUpdate;
public static long minimumDistanceChangeForNetworkUpdate; public static long minimumDistanceChangeForNetworkUpdate;
public static long satisfactoryAccuracyGps; public static long satisfactoryAccuracyGps;
@ -62,6 +65,8 @@ public class Settings implements SharedPreferences
public static boolean noticeAndroid10WifiShown; public static boolean noticeAndroid10WifiShown;
public static long lastNewsPolltime; public static long lastNewsPolltime;
public static ArrayList<String> whatHasBeenDone;
/* /*
Generic settings valid for all installations and not changable Generic settings valid for all installations and not changable
*/ */
@ -250,6 +255,16 @@ public class Settings implements SharedPreferences
noticeAndroid10WifiShown = prefs.getBoolean("noticeAndroid10WifiShown", false); noticeAndroid10WifiShown = prefs.getBoolean("noticeAndroid10WifiShown", false);
lastNewsPolltime = prefs.getLong("lastNewsPolltime", default_lastNewsPolltime); lastNewsPolltime = prefs.getLong("lastNewsPolltime", default_lastNewsPolltime);
String whbdString = prefs.getString("whatHasBeenDone", "");
if(whbdString != null && whbdString.length() > 0)
{
whatHasBeenDone = new ArrayList<>();
for(String s : whbdString.split(";"))
{
whatHasBeenDone.add(s);
}
}
} }
catch(Exception e) catch(Exception e)
{ {
@ -261,6 +276,26 @@ public class Settings implements SharedPreferences
initializeSettings(context, false); initializeSettings(context, false);
} }
} }
public static void considerDone(String key)
{
if(whatHasBeenDone == null)
whatHasBeenDone = new ArrayList<>();
if(!whatHasBeenDone.contains(key))
whatHasBeenDone.add(key);
}
public static boolean hasBeenDone(String key)
{
if(whatHasBeenDone != null)
{
if(whatHasBeenDone.contains(key))
return true;
}
return false;
}
/**Makes sure a settings has a valid setting. If not it will assign a reasonable default setting to it. /**Makes sure a settings has a valid setting. If not it will assign a reasonable default setting to it.
* If force settings will be initialized even if the user has set something.**/ * If force settings will be initialized even if the user has set something.**/
@ -410,6 +445,9 @@ public class Settings implements SharedPreferences
if(!prefs.contains("lastNewsPolltime") | force) if(!prefs.contains("lastNewsPolltime") | force)
editor.putLong("lastNewsPolltime", default_lastNewsPolltime); editor.putLong("lastNewsPolltime", default_lastNewsPolltime);
if(!prefs.contains("whatHasBeenDone") | force)
editor.putString("whatHasBeenDone", "");
editor.commit(); editor.commit();
@ -480,6 +518,8 @@ public class Settings implements SharedPreferences
editor.putLong("lastNewsPolltime", lastNewsPolltime); editor.putLong("lastNewsPolltime", lastNewsPolltime);
editor.putString("whatHasBeenDone", Miscellaneous.explode(";", whatHasBeenDone));
if(lastActivePoi == null) if(lastActivePoi == null)
editor.putString("lastActivePoi", "null"); editor.putString("lastActivePoi", "null");
else else

View File

@ -25,7 +25,8 @@ import java.util.Collections;
public class XmlFileInterface public class XmlFileInterface
{ {
public static File settingsFile = new File(Miscellaneous.getWriteableFolder() + "/Automation_settings.xml"); public static String settingsFileName = "Automation_settings.xml";
public static File settingsFile = new File(Miscellaneous.getWriteableFolder() + "/" + settingsFileName);
public static Context context; public static Context context;
protected static final String encryptionKey = "Y1vsP12L2S3NkTJbDOR4bQ6i02hsoo"; protected static final String encryptionKey = "Y1vsP12L2S3NkTJbDOR4bQ6i02hsoo";

View File

@ -594,4 +594,5 @@
<string name="deviceDoesNotHaveBluetooth">Dieses Gerät scheint kein Bluetooth zu haben. Sie können mit der Konfiguration fortfahren, aber es wird vermutlich keinen Effekt haben.</string> <string name="deviceDoesNotHaveBluetooth">Dieses Gerät scheint kein Bluetooth zu haben. Sie können mit der Konfiguration fortfahren, aber es wird vermutlich keinen Effekt haben.</string>
<string name="manageLocations">Orte anlegen oder ändern</string> <string name="manageLocations">Orte anlegen oder ändern</string>
<string name="publishedOn">veröffentlicht am</string> <string name="publishedOn">veröffentlicht am</string>
<string name="filesHaveBeenMovedTo">Automation benutzt jetzt ein anderes Verzeichnis, um Ihre Daten zu speichern. Alle Ihre Automation-Dateien wurden hierhin verschoben: \"%s\". Die Berechtigung für den externen Speicher wird nun nicht mehr benötigt; Sie können Sie entfernen. In einer künftigen Version wird sie entfernt werden.</string>
</resources> </resources>

View File

@ -605,4 +605,6 @@
<string name="publishedOn">published on</string> <string name="publishedOn">published on</string>
<string name="displayNewsOnMainScreen">Display application news on main screen</string> <string name="displayNewsOnMainScreen">Display application news on main screen</string>
<string name="displayNewsOnMainScreenDescription">Announcements about this app only, we\'re probably talking about 1-2 per year, not more.</string> <string name="displayNewsOnMainScreenDescription">Announcements about this app only, we\'re probably talking about 1-2 per year, not more.</string>
<string name="filesHaveBeenMovedTo">Automation now uses another path to store your files. All your Automation-files have been moved here: \"%s\". The external storage permission is not required anymore; you can revoke it. It will be removed in a future version.</string>
<string name="newsOptIn">Would you like to receive (only important) news about this app on the main screen? Those are downloaded from the developer\'s website. There will be no intrusive notification, just a text on the main screen when you open the app.</string>
</resources> </resources>

View File

@ -0,0 +1,2 @@
Place to storage config files has been moved to regular location (program specific directory in Android/data).
If you had previously used to app your existing files are moved to this new location. If that was successful the permission android.permission.WRITE_EXTERNAL_STORAGE is no longer required.

View File

@ -35,4 +35,53 @@ Supported actions:
* Speak text * Speak text
* Open music player * Open music player
* Change screen brightness * Change screen brightness
* Send text message * Send text message
It's quite hard to keep this app working across the many different hardwares as well as the many changes Android undergoes over the versions. I can test it in the emulator, but that cannot show all bugs.
So if a certain feature is not working on your device - let me know. Over the years I have fixed almost all bugs that have been reported to me. But for that I'm dependend on your input.
A word about the many permissions....
It lies in the nature of this type of application that it requires a lot of permissions. However most of them are entirely optional and are not requested unless one of the rules you created needs it.
Let's go through them quickly:
ACCESS_NETWORK_STATE, CHANGE_NETWORK_STATE: Check or change things like airplane mode, roaming.
ACCESS_WIFI_STATE, CHANGE_WIFI_STATE: Turn wifi on or off
INTERNET
That's required for any of these 3 reasons:
- You are using a locationing method that utilizes CellTowers (default setting)
- You are using triggerUrl as action
- You activate downloading news in settings
BLUETOOTH, BLUETOOTH_ADMIN: Check bluetooth connections or toggle BT on or off.
NFC: Use NFC tags if you created a rule that uses that.
Location (ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION, ACCESS_BACKGROUND_LOCATION): This should be self-explanatory - are you using a rule with locations or speed as trigger?
PROCESS_OUTGOING_CALLS: You can use current calls as trigger. E.g. if wife calls....
SEND_SMS: You can have SMS sent as action. If you choose to do so you can enter the destination number manually or optionally pick one of your contacts which brings us to READ_CONTACTS
READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE: If you initially installed the app after March 2021 this is not required. In the past the app used to store its config file on the regular storage like "sdcard". The permission is still in there to ensure the app is also still working for legacy users. There the app will migrate the files to the new location.
GET_TASKS: For trigger "check if another app is running"
BATTERY_STATS: Check battery level as trigger
MODIFY_AUDIO_SETTINGS, ACCESS_NOTIFICATION_POLICY:
From higher versions on this is required to be able to change, e.g. the ringtone or generally the sound settings.
https://stackoverflow.com/questions/43123650/android-request-access-notification-policy-and-mute-phone/43127589#43127589
ACCESS_NOTIFICATION_POLICY is also included to prepare for a new trigger - to use notifications of other applications as trigger. But this feature has not been implemented, yet.
RECORD_AUDIO: For trigger "check background noise". Btw - my use case for this is: My phone will turn on sounds in the morning. During the week that is quite early. But what if I have a day off? Then it will monitor the background noise as an additional condition. If there's noise it's fairly certain I'm actually awake. Unfortunately Google deactivated this feature with Android 8.
READ_PHONE_STATE: https://developer.android.com/reference/android/Manifest.permission#READ_PHONE_STATE
RECEIVE_BOOT_COMPLETED, FOREGROUND_SERVICE, WAKE_LOCK: Start the service automatically and keep it running
WRITE_SETTINGS: Change system settings
ACCESS_SUPERUSER: root is entirely optional. In the (far) past it used be possible to activate/deactivate USB tethering with regular API calls. But a long time ago this started to be possible using root only. So long story short: It's only necessary for some specific features.