News download finished, but multilanguage news still pending.
This commit is contained in:
		| @@ -22,10 +22,14 @@ import android.widget.TextView; | ||||
| import android.widget.Toast; | ||||
| import android.widget.ToggleButton; | ||||
|  | ||||
| import androidx.core.text.HtmlCompat; | ||||
|  | ||||
| import com.jens.automation2.AutomationService.serviceCommands; | ||||
| import com.jens.automation2.Trigger.Trigger_Enum; | ||||
| import com.jens.automation2.location.LocationProvider; | ||||
|  | ||||
| import java.lang.reflect.Array; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Calendar; | ||||
|  | ||||
| @SuppressLint("NewApi") | ||||
| @@ -36,7 +40,7 @@ public class ActivityMainScreen extends ActivityGeneric | ||||
| 	private static ActivityMainScreen activityMainScreenInstance = null; | ||||
| 	private ToggleButton toggleService, tbLockSound; | ||||
| 	private Button bShowHelp, bPrivacy, bSettingsErase, bSettingsSetToDefault, bVolumeTest, bAddSoundLockTIme; | ||||
| 	private TextView tvActivePoi, tvClosestPoi, tvLastRule, tvMainScreenNote, tvMainScreenNote2, tvlockSoundDuration; | ||||
| 	private TextView tvActivePoi, tvClosestPoi, tvLastRule, tvMainScreenNote1, tvMainScreenNote2, tvMainScreenNote3, tvlockSoundDuration; | ||||
|  | ||||
| 	private ListView lvRuleHistory; | ||||
| 	private ArrayAdapter<Rule> ruleHistoryListViewAdapter; | ||||
| @@ -65,8 +69,9 @@ public class ActivityMainScreen extends ActivityGeneric | ||||
| 		tvClosestPoi = (TextView) findViewById(R.id.tvClosestPoi); | ||||
| 		lvRuleHistory = (ListView) findViewById(R.id.lvRuleHistory); | ||||
| 		tvLastRule = (TextView) findViewById(R.id.tvTimeFrameHelpText); | ||||
| 		tvMainScreenNote = (TextView) findViewById(R.id.tvMainScreenNote); | ||||
| 		tvMainScreenNote1 = (TextView) findViewById(R.id.tvMainScreenNote1); | ||||
| 		tvMainScreenNote2 = (TextView) findViewById(R.id.tvMainScreenNote2); | ||||
| 		tvMainScreenNote3 = (TextView) findViewById(R.id.tvMainScreenNote3); | ||||
| 		tvlockSoundDuration = (TextView)findViewById(R.id.tvlockSoundDuration); | ||||
| 		tbLockSound = (ToggleButton) findViewById(R.id.tbLockSound); | ||||
| 		toggleService = (ToggleButton) findViewById(R.id.tbArmMastListener); | ||||
| @@ -89,7 +94,7 @@ public class ActivityMainScreen extends ActivityGeneric | ||||
| 			} | ||||
| 		}); | ||||
|  | ||||
| 		tvMainScreenNote.setOnClickListener(new OnClickListener() | ||||
| 		tvMainScreenNote1.setOnClickListener(new OnClickListener() | ||||
| 		{ | ||||
| 			@Override | ||||
| 			public void onClick(View v) | ||||
| @@ -285,13 +290,13 @@ public class ActivityMainScreen extends ActivityGeneric | ||||
| 		{ | ||||
| 			if(ActivityPermissions.needMorePermissions(activityMainScreenInstance)) | ||||
| 			{ | ||||
| 				activityMainScreenInstance.tvMainScreenNote.setText(R.string.mainScreenPermissionNote); | ||||
| 				activityMainScreenInstance.tvMainScreenNote.setVisibility(View.VISIBLE); | ||||
| 				activityMainScreenInstance.tvMainScreenNote1.setText(R.string.mainScreenPermissionNote); | ||||
| 				activityMainScreenInstance.tvMainScreenNote1.setVisibility(View.VISIBLE); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				activityMainScreenInstance.tvMainScreenNote.setText(""); | ||||
| 				activityMainScreenInstance.tvMainScreenNote.setVisibility(View.GONE); | ||||
| 				activityMainScreenInstance.tvMainScreenNote1.setText(""); | ||||
| 				activityMainScreenInstance.tvMainScreenNote1.setVisibility(View.GONE); | ||||
| 			} | ||||
|  | ||||
| 			if(Miscellaneous.restrictedFeaturesConfigured()) | ||||
| @@ -411,7 +416,8 @@ public class ActivityMainScreen extends ActivityGeneric | ||||
| 		else | ||||
| 			Miscellaneous.logEvent("i", "ActivityMainScreen", "Activity not running. No need to update.", 5); | ||||
|  | ||||
| //		activityMainScreenInstance.checkForNews(); | ||||
| 		if(activityMainScreenInstance != null) | ||||
| 			activityMainScreenInstance.checkForNews(); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| @@ -563,11 +569,49 @@ public class ActivityMainScreen extends ActivityGeneric | ||||
| 		Miscellaneous.messageBox(title, text, ActivityMainScreen.getActivityMainScreenInstance()); | ||||
| 	} | ||||
|  | ||||
| 	void checkForNews() | ||||
| 	synchronized void checkForNews() | ||||
| 	{ | ||||
| 		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); | ||||
| 		News.AsyncTaskDownloadNews dnTask = new News.AsyncTaskDownloadNews(); | ||||
| 		dnTask.execute(ActivityMainScreen.this); | ||||
|  | ||||
| 		StrictMode.setThreadPolicy(policy); | ||||
| 		News.downloadNews(); | ||||
| //		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); | ||||
| //		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) | ||||
| 	{ | ||||
| 		if (newsToDisplay.size() > 0) | ||||
| 		{ | ||||
| 			activityMainScreenInstance.tvMainScreenNote3.setText(HtmlCompat.fromHtml(newsToDisplay.get(0).toStringHtml(), 0)); | ||||
| 			activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.VISIBLE); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			activityMainScreenInstance.tvMainScreenNote3.setText(""); | ||||
| 			activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.GONE); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -47,6 +47,7 @@ import org.xml.sax.SAXException; | ||||
| import java.io.BufferedReader; | ||||
| import java.io.BufferedWriter; | ||||
| import java.io.File; | ||||
| import java.io.FileNotFoundException; | ||||
| import java.io.FileWriter; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| @@ -68,6 +69,7 @@ import java.sql.Time; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| import java.util.Scanner; | ||||
|  | ||||
| import javax.net.ssl.HostnameVerifier; | ||||
| import javax.net.ssl.HttpsURLConnection; | ||||
| @@ -989,4 +991,50 @@ public class Miscellaneous extends Service | ||||
| 		root.getChildNodes(); | ||||
| 	*/ | ||||
| 	} | ||||
|  | ||||
| 	public static Calendar calendarFromLong(long input) | ||||
| 	{ | ||||
| 		Calendar returnValue = Calendar.getInstance(); | ||||
| 		returnValue.setTimeInMillis(input); | ||||
| 		return returnValue; | ||||
| 	} | ||||
|  | ||||
| 	public static boolean writeStringToFile(String filename, String input) | ||||
| 	{ | ||||
| 		try | ||||
| 		{ | ||||
| 			FileWriter myWriter = new FileWriter(filename); | ||||
| 			myWriter.write(input); | ||||
| 			myWriter.close(); | ||||
| 			return true; | ||||
| 		} | ||||
| 		catch (IOException e) | ||||
| 		{ | ||||
| 			Miscellaneous.logEvent("e", "Error writing to file", Log.getStackTraceString(e), 3); | ||||
| 			return false; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public static String readFileToString(String fileName) | ||||
| 	{ | ||||
| 		try | ||||
| 		{ | ||||
| 			StringBuilder result = new StringBuilder(); | ||||
| 			File myObj = new File(fileName); | ||||
| 			Scanner myReader = new Scanner(myObj); | ||||
| 			while (myReader.hasNextLine()) | ||||
| 			{ | ||||
| 				String data = myReader.nextLine(); | ||||
| 				result.append(data); | ||||
| 			} | ||||
| 			myReader.close(); | ||||
|  | ||||
| 			return result.toString(); | ||||
| 		} | ||||
| 		catch (FileNotFoundException e) | ||||
| 		{ | ||||
| 			Miscellaneous.logEvent("e", "Error reading file " + fileName, Log.getStackTraceString(e), 3); | ||||
| 			return null; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -1,12 +1,20 @@ | ||||
| package com.jens.automation2; | ||||
|  | ||||
| import android.content.Context; | ||||
| import android.os.AsyncTask; | ||||
|  | ||||
| import androidx.annotation.NonNull; | ||||
|  | ||||
| import org.w3c.dom.Element; | ||||
| import org.w3c.dom.Node; | ||||
| import org.w3c.dom.NodeList; | ||||
| import org.xml.sax.SAXException; | ||||
|  | ||||
| import java.lang.reflect.Array; | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
|  | ||||
| public class News | ||||
| { | ||||
| @@ -15,10 +23,28 @@ public class News | ||||
|     Calendar publishDate; | ||||
|     String applicablePlattform; | ||||
|  | ||||
|     public static ArrayList<News> downloadNews() | ||||
|     public static ArrayList<News> downloadNews(Context context) | ||||
|     { | ||||
|         String newsUrl = "https://server47.de/automation/appNews.php"; | ||||
|         String newsContent = Miscellaneous.downloadURL(newsUrl, null, null); | ||||
|         Calendar now = Calendar.getInstance(); | ||||
|         String newsContent; | ||||
|  | ||||
|         if (Settings.lastNewsPolltime == -1 || now.getTimeInMillis() >= Settings.lastNewsPolltime + (long)(Settings.pollNewsEveryXDays * 24 * 60 * 60 * 1000)) | ||||
|         { | ||||
|             String newsUrl = "https://server47.de/automation/appNews.php"; | ||||
|             newsContent = Miscellaneous.downloadURL(newsUrl, null, null); | ||||
|  | ||||
|             // Cache content to local storage | ||||
|             if(Miscellaneous.writeStringToFile(context.getFilesDir() + "/appNews.xml", newsContent)) | ||||
|             { | ||||
|                 Settings.lastNewsPolltime = now.getTimeInMillis(); | ||||
|                 Settings.writeSettings(context); | ||||
|             } | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             // Just read local cache file | ||||
|             newsContent = Miscellaneous.readFileToString(context.getFilesDir() + "/appNews.xml"); | ||||
|         } | ||||
|  | ||||
|         ArrayList<News> returnList = new ArrayList<>(); | ||||
|  | ||||
| @@ -41,13 +67,12 @@ public class News | ||||
|                     newsEntry.setText(neEl.getElementsByTagName("text").item(0).getTextContent()); | ||||
|  | ||||
|                     String publishDateString = neEl.getElementsByTagName("publishDate").item(0).getTextContent(); | ||||
|                     Calendar cal = Calendar.getInstance(); | ||||
|                     cal.setTimeInMillis(Long.parseLong(publishDateString)); | ||||
|                     newsEntry.setPublishDate(cal); | ||||
|                     newsEntry.setPublishDate(Miscellaneous.calendarFromLong(Long.parseLong(publishDateString) * 1000)); | ||||
|  | ||||
|                     newsEntry.setApplicablePlattform(neEl.getElementsByTagName("applicablePlattforms").item(0).getTextContent()); | ||||
|  | ||||
|                     returnList.add(newsEntry); | ||||
|                     if(newsEntry.getApplicablePlattform().equalsIgnoreCase("all") || newsEntry.getApplicablePlattform().equalsIgnoreCase(BuildConfig.FLAVOR)) | ||||
|                         returnList.add(newsEntry); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| @@ -59,6 +84,19 @@ public class News | ||||
|         return returnList; | ||||
|     } | ||||
|  | ||||
|     public static ArrayList<News> downloadNews(Context context, Calendar ageLimit) | ||||
|     { | ||||
|         ArrayList<News> returnList = new ArrayList<>(); | ||||
|  | ||||
|         for(News newsEntry : downloadNews(context)) | ||||
|         { | ||||
|             if (newsEntry.getPublishDate().getTimeInMillis() >= ageLimit.getTimeInMillis()) | ||||
|                 returnList.add(newsEntry); | ||||
|         } | ||||
|  | ||||
|         return returnList; | ||||
|     } | ||||
|  | ||||
|     public String getHeadline() | ||||
|     { | ||||
|         return headline; | ||||
| @@ -98,4 +136,41 @@ public class News | ||||
|     { | ||||
|         this.applicablePlattform = applicablePlattform; | ||||
|     } | ||||
|  | ||||
|     @NonNull | ||||
|     @Override | ||||
|     public String toString() | ||||
|     { | ||||
|         SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");; | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         Date now = this.getPublishDate().getTime(); | ||||
|         String timestamp = sdf.format(now); | ||||
|  | ||||
|         return this.getHeadline() + " published on " + timestamp + Miscellaneous.lineSeparator + this.getText(); | ||||
|     } | ||||
|  | ||||
|     public String toStringHtml() | ||||
|     { | ||||
|         SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");; | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         Date now = this.getPublishDate().getTime(); | ||||
|         String timestamp = sdf.format(now); | ||||
|  | ||||
|         return "<b><u><i>" + this.getHeadline() + "</i></u></b>" + " published on " + timestamp + "<br>" + this.getText(); | ||||
|     } | ||||
|  | ||||
|     public static class AsyncTaskDownloadNews extends AsyncTask<Context, Void, ArrayList> | ||||
|     { | ||||
|         @Override | ||||
|         protected ArrayList doInBackground(Context... contexts) | ||||
|         { | ||||
|             return downloadNews(contexts[0]); | ||||
|         } | ||||
|  | ||||
|         @Override | ||||
|         protected void onPostExecute(ArrayList arrayList) | ||||
|         { | ||||
|             ActivityMainScreen.getActivityMainScreenInstance().processNewsResult(arrayList); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -11,6 +11,8 @@ import java.util.Set; | ||||
| public class Settings implements SharedPreferences | ||||
| { | ||||
| 	public static final int rulesThatHaveBeenRanHistorySize = 10; | ||||
| 	public final static int lockSoundChangesInterval = 15; | ||||
| 	public static final int pollNewsEveryXDays = 7; | ||||
| 	public static final String folderName = "Automation"; | ||||
|  | ||||
| 	public static long minimumDistanceChangeForGpsUpdate; | ||||
| @@ -56,6 +58,7 @@ public class Settings implements SharedPreferences | ||||
| 	public static boolean lockSoundChanges; | ||||
| 	public static boolean noticeAndroid9MicrophoneShown; | ||||
| 	public static boolean noticeAndroid10WifiShown; | ||||
| 	public static long lastNewsPolltime; | ||||
|  | ||||
| 	/* | ||||
| 		Generic settings valid for all installations and not changable | ||||
| @@ -103,7 +106,7 @@ public class Settings implements SharedPreferences | ||||
| 	protected static final int default_startScreen = 0; | ||||
| 	protected static final boolean default_executeRulesAndProfilesWithSingleClick = false; | ||||
| 	protected static final boolean default_lockSoundChanges = false; | ||||
|     public final static int lockSoundChangesInterval = 15; | ||||
| 	protected static final long default_lastNewsPolltime = -1; | ||||
|  | ||||
|     @Override | ||||
| 	public boolean contains(String arg0) | ||||
| @@ -241,6 +244,8 @@ public class Settings implements SharedPreferences | ||||
| 			lockSoundChanges = prefs.getBoolean("lockSoundChanges", default_lockSoundChanges); | ||||
| 			noticeAndroid9MicrophoneShown = prefs.getBoolean("noticeAndroid9MicrophoneShown", false); | ||||
| 			noticeAndroid10WifiShown = prefs.getBoolean("noticeAndroid10WifiShown", false); | ||||
|  | ||||
| 			lastNewsPolltime = prefs.getLong("lastNewsPolltime", default_lastNewsPolltime); | ||||
| 		} | ||||
| 		catch(Exception e) | ||||
| 		{ | ||||
| @@ -395,6 +400,9 @@ public class Settings implements SharedPreferences | ||||
|  | ||||
| 			if(!prefs.contains("noticeAndroid9MicrophoneShown") | force) | ||||
| 				editor.putBoolean("noticeAndroid9MicrophoneShown", false); | ||||
|  | ||||
| 			if(!prefs.contains("lastNewsPolltime") | force) | ||||
| 				editor.putLong("lastNewsPolltime", default_lastNewsPolltime); | ||||
| 			 | ||||
| 			editor.commit(); | ||||
| 			 | ||||
| @@ -462,6 +470,8 @@ public class Settings implements SharedPreferences | ||||
| 				editor.putBoolean("noticeAndroid9MicrophoneShown", noticeAndroid9MicrophoneShown); | ||||
| 				editor.putBoolean("noticeAndroid10WifiShown", noticeAndroid10WifiShown); | ||||
|  | ||||
| 				editor.putLong("lastNewsPolltime", lastNewsPolltime); | ||||
|  | ||||
| 				if(lastActivePoi == null) | ||||
| 					editor.putString("lastActivePoi", "null"); | ||||
| 				else | ||||
|   | ||||
| @@ -27,7 +27,7 @@ | ||||
| 		</LinearLayout> | ||||
|  | ||||
| 		<TextView | ||||
| 			android:id="@+id/tvMainScreenNote" | ||||
| 			android:id="@+id/tvMainScreenNote1" | ||||
| 			android:layout_width="match_parent" | ||||
| 			android:layout_height="wrap_content" | ||||
| 			android:gravity="center" | ||||
| @@ -36,6 +36,7 @@ | ||||
| 			android:scrollHorizontally="false" | ||||
| 			android:textColor="@color/importantMessage" | ||||
| 			android:singleLine="false" | ||||
| 			android:visibility="gone" | ||||
| 			android:textAppearance="?android:attr/textAppearanceMedium" /> | ||||
|  | ||||
| 		<TextView | ||||
| @@ -48,6 +49,21 @@ | ||||
| 			android:scrollHorizontally="false" | ||||
| 			android:textColor="@color/importantMessage" | ||||
| 			android:singleLine="false" | ||||
| 			android:visibility="gone" | ||||
| 			android:textAppearance="?android:attr/textAppearanceMedium" /> | ||||
|  | ||||
| 		<TextView | ||||
| 			android:id="@+id/tvMainScreenNote3" | ||||
| 			android:layout_width="match_parent" | ||||
| 			android:layout_height="wrap_content" | ||||
| 			android:gravity="center" | ||||
| 			android:layout_marginLeft="10dp" | ||||
| 			android:layout_marginRight="10dp" | ||||
|  | ||||
| 			android:scrollHorizontally="false" | ||||
| 			android:textColor="@color/newsMessage" | ||||
| 			android:singleLine="false" | ||||
| 			android:visibility="gone" | ||||
| 			android:textAppearance="?android:attr/textAppearanceMedium" /> | ||||
| 		 | ||||
| 	    <TableLayout  | ||||
|   | ||||
| @@ -2,6 +2,7 @@ | ||||
| <resources> | ||||
|     <color name="barBackgroundColor">#dddddd</color> | ||||
|     <color name="importantMessage">#ff0000</color> | ||||
|     <color name="newsMessage">#00008B</color> | ||||
|     <color name="white">#FFFFFF</color> | ||||
|     <color name="purple_700">#800080</color> | ||||
|     <color name="purple_500">#800080</color> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user