2021-02-20 02:04:53 +01:00
package com.jens.automation2 ;
2021-02-20 13:52:15 +01:00
import android.content.Context ;
import android.os.AsyncTask ;
2021-02-21 23:13:11 +01:00
import android.util.Log ;
2021-02-20 13:52:15 +01:00
import androidx.annotation.NonNull ;
2021-02-20 02:04:53 +01:00
import org.w3c.dom.Element ;
2021-02-21 23:13:11 +01:00
import org.w3c.dom.NamedNodeMap ;
2021-02-20 02:04:53 +01:00
import org.w3c.dom.Node ;
import org.w3c.dom.NodeList ;
2021-02-21 19:21:40 +01:00
import java.io.File ;
2021-02-20 13:52:15 +01:00
import java.text.SimpleDateFormat ;
2021-02-20 02:04:53 +01:00
import java.util.ArrayList ;
import java.util.Calendar ;
2021-02-20 13:52:15 +01:00
import java.util.Date ;
2021-02-21 23:13:11 +01:00
import java.util.HashMap ;
import java.util.Locale ;
import java.util.Map ;
2021-02-20 02:04:53 +01:00
public class News
{
Calendar publishDate ;
2021-12-20 19:58:43 +01:00
String applicablePlatform ;
2021-02-21 23:13:11 +01:00
Map < String , NewsTranslation > translations = new HashMap < > ( ) ;
public static class NewsTranslation
{
String language ;
String headline ;
String text ;
public String getLanguage ( )
{
return language ;
}
public void setLanguage ( String language )
{
this . language = language ;
}
public String getHeadline ( )
{
return headline ;
}
public void setHeadline ( String headline )
{
this . headline = headline ;
}
public String getText ( )
{
return text ;
}
public void setText ( String text )
{
this . text = text ;
}
}
2021-02-20 02:04:53 +01:00
2021-02-20 13:52:15 +01:00
public static ArrayList < News > downloadNews ( Context context )
2021-02-20 02:04:53 +01:00
{
2021-02-20 13:52:15 +01:00
Calendar now = Calendar . getInstance ( ) ;
String newsContent ;
2021-03-29 16:36:21 +02:00
String newsFileName = " appNews.xml " ;
String filePath = context . getCacheDir ( ) + " / " + newsFileName ;
File oldFilePath = new File ( context . getFilesDir ( ) + " / " + newsFileName ) ;
if ( oldFilePath . exists ( ) )
oldFilePath . delete ( ) ;
2021-02-21 19:21:40 +01:00
2021-06-08 23:14:09 +02:00
if ( ! ( new File ( filePath ) ) . exists ( ) | | Settings . lastNewsPolltime = = Settings . default_lastNewsPolltime | | now . getTimeInMillis ( ) > = Settings . lastNewsPolltime + ( long ) ( Settings . newsDisplayForXDays * 24 * 60 * 60 * 1000 ) )
2021-02-20 13:52:15 +01:00
{
String newsUrl = " https://server47.de/automation/appNews.php " ;
newsContent = Miscellaneous . downloadURL ( newsUrl , null , null ) ;
// Cache content to local storage
2021-02-21 19:21:40 +01:00
if ( Miscellaneous . writeStringToFile ( filePath , newsContent ) )
2021-02-20 13:52:15 +01:00
{
Settings . lastNewsPolltime = now . getTimeInMillis ( ) ;
Settings . writeSettings ( context ) ;
2021-03-29 16:36:21 +02:00
Miscellaneous . logEvent ( " i " , newsFileName , " File stored to " + filePath , 5 ) ;
2021-02-20 13:52:15 +01:00
}
}
else
{
// Just read local cache file
2021-02-21 19:21:40 +01:00
newsContent = Miscellaneous . readFileToString ( filePath ) ;
2021-03-29 16:36:21 +02:00
Miscellaneous . logEvent ( " i " , newsFileName , " Using cache to retrieve news: " + filePath , 5 ) ;
2021-02-20 13:52:15 +01:00
}
2021-02-20 02:04:53 +01:00
2021-02-20 02:30:06 +01:00
ArrayList < News > returnList = new ArrayList < > ( ) ;
try
2021-02-20 02:04:53 +01:00
{
2021-02-20 02:30:06 +01:00
Element automationRootElement = Miscellaneous . getXmlTree ( newsContent ) ;
NodeList newsEntriesElements = automationRootElement . getElementsByTagName ( " newsEntries " ) ;
NodeList newsEntryElements = ( ( Element ) newsEntriesElements . item ( 0 ) ) . getElementsByTagName ( " newsEntry " ) ;
for ( int i = 0 ; i < newsEntryElements . getLength ( ) ; i + + )
2021-02-20 02:04:53 +01:00
{
2021-02-20 02:30:06 +01:00
if ( newsEntryElements . item ( i ) . getNodeType ( ) = = Node . ELEMENT_NODE & & ( newsEntryElements . item ( i ) . getParentNode ( ) . isSameNode ( ( ( Element ) newsEntriesElements . item ( 0 ) ) ) ) )
2021-02-20 02:04:53 +01:00
{
2021-02-20 02:30:06 +01:00
News newsEntry = new News ( ) ;
Element neEl = ( Element ) newsEntryElements . item ( i ) ;
2021-02-21 23:13:11 +01:00
NodeList headLineNodes = neEl . getElementsByTagName ( " headline " ) ;
for ( int h = 0 ; h < headLineNodes . getLength ( ) ; h + + )
{
NamedNodeMap attrMap = headLineNodes . item ( h ) . getAttributes ( ) ;
for ( int n = 0 ; n < attrMap . getLength ( ) ; n + + )
{
if ( attrMap . item ( n ) . getNodeName ( ) . equalsIgnoreCase ( " language " ) )
{
String language = attrMap . item ( n ) . getTextContent ( ) ;
if ( ! newsEntry . getTranslations ( ) . containsKey ( language ) )
newsEntry . getTranslations ( ) . put ( language , new NewsTranslation ( ) ) ;
newsEntry . getTranslations ( ) . get ( language ) . setHeadline ( headLineNodes . item ( h ) . getTextContent ( ) ) ;
}
}
}
NodeList textNodes = neEl . getElementsByTagName ( " text " ) ;
for ( int t = 0 ; t < textNodes . getLength ( ) ; t + + )
{
NamedNodeMap attrMap = textNodes . item ( t ) . getAttributes ( ) ;
for ( int n = 0 ; n < attrMap . getLength ( ) ; n + + )
{
if ( attrMap . item ( n ) . getNodeName ( ) . equalsIgnoreCase ( " language " ) )
{
String language = attrMap . item ( n ) . getTextContent ( ) ;
if ( ! newsEntry . getTranslations ( ) . containsKey ( language ) )
newsEntry . getTranslations ( ) . put ( language , new NewsTranslation ( ) ) ;
newsEntry . getTranslations ( ) . get ( language ) . setText ( textNodes . item ( t ) . getTextContent ( ) ) ;
}
}
}
2021-02-20 02:30:06 +01:00
String publishDateString = neEl . getElementsByTagName ( " publishDate " ) . item ( 0 ) . getTextContent ( ) ;
2021-02-20 13:52:15 +01:00
newsEntry . setPublishDate ( Miscellaneous . calendarFromLong ( Long . parseLong ( publishDateString ) * 1000 ) ) ;
2021-02-20 02:30:06 +01:00
2021-12-20 19:58:43 +01:00
newsEntry . setApplicablePlatform ( neEl . getElementsByTagName ( " applicablePlattforms " ) . item ( 0 ) . getTextContent ( ) ) ;
2021-02-20 02:30:06 +01:00
2021-12-20 19:58:43 +01:00
if ( newsEntry . getApplicablePlatform ( ) . equalsIgnoreCase ( " all " ) | | newsEntry . getApplicablePlatform ( ) . equalsIgnoreCase ( BuildConfig . FLAVOR ) )
2021-02-20 13:52:15 +01:00
returnList . add ( newsEntry ) ;
2021-02-20 02:04:53 +01:00
}
}
}
2021-02-20 02:30:06 +01:00
catch ( Exception e )
{
e . printStackTrace ( ) ;
}
return returnList ;
}
2021-02-20 13:52:15 +01:00
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 ;
}
2021-02-21 23:13:11 +01:00
public Map < String , NewsTranslation > getTranslations ( )
2021-02-20 02:30:06 +01:00
{
2021-02-21 23:13:11 +01:00
return translations ;
2021-02-20 02:30:06 +01:00
}
2021-02-21 23:13:11 +01:00
public void setTranslations ( Map < String , NewsTranslation > translations )
2021-02-20 02:30:06 +01:00
{
2021-02-21 23:13:11 +01:00
this . translations = translations ;
2021-02-20 02:30:06 +01:00
}
public Calendar getPublishDate ( )
{
return publishDate ;
}
public void setPublishDate ( Calendar publishDate )
{
this . publishDate = publishDate ;
}
2021-12-20 19:58:43 +01:00
public String getApplicablePlatform ( )
2021-02-20 02:30:06 +01:00
{
2021-12-20 19:58:43 +01:00
return applicablePlatform ;
2021-02-20 02:30:06 +01:00
}
2021-12-20 19:58:43 +01:00
public void setApplicablePlatform ( String applicablePlatform )
2021-02-20 02:30:06 +01:00
{
2021-12-20 19:58:43 +01:00
this . applicablePlatform = applicablePlatform ;
2021-02-20 02:30:06 +01:00
}
2021-02-20 13:52:15 +01:00
@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 ) ;
2021-02-21 23:13:11 +01:00
String langToChoose = " en " ;
String systemLanguage = Locale . getDefault ( ) . getLanguage ( ) ;
if ( this . getTranslations ( ) . containsKey ( systemLanguage ) )
langToChoose = systemLanguage ;
return this . getTranslations ( ) . get ( langToChoose ) . getHeadline ( ) + " " + Miscellaneous . getAnyContext ( ) . getString ( R . string . publishedOn ) + " " + timestamp + Miscellaneous . lineSeparator + this . getTranslations ( ) . get ( langToChoose ) . getText ( ) ;
2021-02-20 13:52:15 +01:00
}
public String toStringHtml ( )
{
SimpleDateFormat sdf = new SimpleDateFormat ( " dd.MM.yyyy " ) ; ;
Calendar calendar = Calendar . getInstance ( ) ;
Date now = this . getPublishDate ( ) . getTime ( ) ;
String timestamp = sdf . format ( now ) ;
2021-02-21 23:13:11 +01:00
String langToChoose = " en " ;
String systemLanguage = Locale . getDefault ( ) . getLanguage ( ) ;
if ( this . getTranslations ( ) . containsKey ( systemLanguage ) )
langToChoose = systemLanguage ;
return " <b><u><i> " + this . getTranslations ( ) . get ( langToChoose ) . getHeadline ( ) + " </i></u></b> " + " " + Miscellaneous . getAnyContext ( ) . getString ( R . string . publishedOn ) + " " + timestamp + " <br> " + this . getTranslations ( ) . get ( langToChoose ) . getText ( ) ;
2021-02-20 13:52:15 +01:00
}
public static class AsyncTaskDownloadNews extends AsyncTask < Context , Void , ArrayList >
{
@Override
protected ArrayList doInBackground ( Context . . . contexts )
{
2021-02-21 23:13:11 +01:00
try
{
Calendar limit = Calendar . getInstance ( ) ;
2021-03-02 19:55:43 +01:00
limit . add ( Calendar . DAY_OF_MONTH , - Settings . newsPollEveryXDays ) ;
2021-02-21 23:13:11 +01:00
return downloadNews ( contexts [ 0 ] , limit ) ;
}
catch ( Exception e )
{
Miscellaneous . logEvent ( " e " , " Error displaying news " , Log . getStackTraceString ( e ) , 3 ) ;
return new ArrayList ( ) ;
}
2021-02-20 13:52:15 +01:00
}
@Override
protected void onPostExecute ( ArrayList arrayList )
{
2021-03-20 12:16:39 +01:00
try
{
ActivityMainScreen . getActivityMainScreenInstance ( ) . processNewsResult ( arrayList ) ;
}
catch ( NullPointerException e )
{
Miscellaneous . logEvent ( " e " , " NewsDownload " , " There was a problem displaying the already downloded news, probably ActivityMainScreen isn't currently shown: " + Log . getStackTraceString ( e ) , 2 ) ;
}
catch ( Exception e )
{
Miscellaneous . logEvent ( " e " , " NewsDownload " , " There was a problem displaying the already downloded news: " + Log . getStackTraceString ( e ) , 2 ) ;
}
2021-02-20 13:52:15 +01:00
}
}
2021-02-21 23:13:11 +01:00
}