Commit 4007fe38 authored by Daniel Niecke's avatar Daniel Niecke

closing #8

parent 85cdf57b
/server/test.db
gradle
.vscode
.idea
*.iml
.gradle
......
......@@ -37,7 +37,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation "com.android.support:support-compat:28.0.0"
implementation 'com.android.support:support-compat:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
......@@ -46,5 +46,7 @@ dependencies {
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
implementation 'eu.the4thfloor.volley:com.android.volley:2015.05.28'
implementation 'com.jakewharton.timber:timber:4.5.1'
implementation 'log4j:log4j:1.2.17'
implementation 'de.mindpipe.android:android-logging-log4j:1.0.3'
implementation 'org.jetbrains:annotations-java5:15.0'
}
......@@ -32,12 +32,6 @@
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCkr1QTUNguy37E-1LXQ4igD3Icjo01x0g" />
<receiver android:name=".autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
package de.niecke_it.gps_tracker;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.*;
import android.util.Log;
import android.os.Build;
import java.util.Calendar;
import java.util.Random;
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = MainActivity.class.getSimpleName();
private org.apache.log4j.Logger log;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Alarm received; waking GPSService");
GPSService.enqueueWork(context, new Intent());
log = GPSLogger.getLogger("GPSService" );
Intent new_intent = new Intent(context, GPSService.class);
Random r = new Random();
int work_id = r.nextInt(100000);
log.debug("{WORK_ID: " + work_id + "} - Alarm received; waking GPSService");
new_intent.putExtra("WORK_ID", work_id);
//GPSService.enqueueWork(context, new_intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new_intent);
} else {
context.startService(new_intent);
}
// Setup new exact alarm in 1 minute
Intent alarm = new Intent(context, AlarmReceiver.class);
log.debug("{WORK_ID: " + work_id + "} - Setting new alarm.");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 99, alarm, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+60000, pendingIntent);
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+2000, 60000, pendingIntent);
log.debug("Set alarm from AlarmReceiver");
log.debug("{WORK_ID: " + work_id + "} - DONE.");
}
}
package de.niecke_it.gps_tracker;
import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Locale;
import timber.log.Timber;
public class FileLoggingTree extends Timber.DebugTree {
private static final String TAG = FileLoggingTree.class.getSimpleName();
private Context context;
public FileLoggingTree(Context context) {
this.context = context;
}
@Override
protected void log(int priority, String tag, String message, Throwable t) {
try {
File direct = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/YoScholarDeliveryLogs");
if (!direct.exists()) {
direct.mkdir();
}
String fileNameTimeStamp = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
String logTimeStamp = new SimpleDateFormat("E MMM dd yyyy 'at' hh:mm:ss:SSS aaa", Locale.getDefault()).format(new Date
());
String fileName = fileNameTimeStamp + ".html";
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/YoScholarDeliveryLogs" + File.separator + fileName);
file.createNewFile();
if (file.exists()) {
OutputStream fileOutputStream = new FileOutputStream(file, true);
fileOutputStream.write(("<p style=\"background:lightgray;\"><strong style=\"background:lightblue;\">&nbsp&nbsp" + logTimeStamp + " :&nbsp&nbsp</strong>&nbsp&nbsp" + message + "</p>").getBytes());
fileOutputStream.close();
}
//if (context != null)
//MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
} catch (Exception e) {
Log.e(TAG, "Error while logging into file : " + e);
}
}
}
\ No newline at end of file
package de.niecke_it.gps_tracker;
import android.os.Environment;
import android.util.Log;
import de.mindpipe.android.logging.log4j.LogConfigurator;
class GPSLogger {
private final static LogConfigurator mLogConfigrator = new LogConfigurator();
static {
configureLog4j();
}
private static void configureLog4j() {
String fileName = Environment.getExternalStorageDirectory() + "/Download/" + "log4j.log";
Log.e("LOGGER", "filename: " + fileName);
// See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
String filePattern = "%d - [%c] - %p : %m%n";
int maxBackupSize = 10;
long maxFileSize = 1024 * 1024;
configure(fileName, filePattern, maxBackupSize, maxFileSize );
}
private static void configure(String fileName, String filePattern, int maxBackupSize, long maxFileSize) {
mLogConfigrator.setFileName(fileName);
mLogConfigrator.setMaxFileSize(maxFileSize);
mLogConfigrator.setFilePattern(filePattern);
mLogConfigrator.setMaxBackupSize(maxBackupSize);
mLogConfigrator.setUseLogCatAppender(true);
mLogConfigrator.configure();
}
static org.apache.log4j.Logger getLogger(String name) {
return org.apache.log4j.Logger.getLogger(name);
}
}
......@@ -5,7 +5,6 @@ import android.annotation.SuppressLint;
import android.app.*;
import android.content.*;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.*;
import android.os.*;
import android.support.v4.app.ActivityCompat;
......@@ -34,6 +33,7 @@ public class MainActivity extends Activity
private Marker marker = null;
private Circle circle = null;
private Marker tmp_marker = null;
private org.apache.log4j.Logger log;
/** Called when the user taps the Send button */
public void showOptions(View view) {
......@@ -86,16 +86,29 @@ public class MainActivity extends Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// check for permissions when the app is started
String[] PERMISSIONS = {
String[] PERMISSIONS = new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.INTERNET};
Manifest.permission.INTERNET,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
PERMISSIONS = new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.INTERNET,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.FOREGROUND_SERVICE};
}
if (hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST);
}
checkExternalMedia();
log = GPSLogger.getLogger("MainActivity" );
sharedPreferences = getSharedPreferences("GPSService", Context.MODE_PRIVATE);
// when not activated setup default radius and show options_activity
......@@ -122,7 +135,8 @@ public class MainActivity extends Activity
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 99, alarm, 0);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+2000, 60000, pendingIntent);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+60000, pendingIntent);
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+2000, 60000, pendingIntent);
Log.e(TAG, "Set alarm");
}
}
......@@ -242,6 +256,28 @@ public class MainActivity extends Activity
return false;
}
private void checkExternalMedia(){
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(mContext,"External Media: readable="
+mExternalStorageAvailable+" writable="+mExternalStorageWriteable , duration);
toast.show();
}
private static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
......
package de.niecke_it.gps_tracker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
public class Restarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*
Log.i("Broadcast Listened", "Service tried to stop");
Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, GPSService.class));
} else {
context.startService(new Intent(context, GPSService.class));
}
*/
}
}
\ No newline at end of file
package de.niecke_it.gps_tracker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class autostart extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent arg1)
{
/*
Intent intent = new Intent(context, GPSService.class);
context.startService(intent);
Log.i("Autostart", "started");
*/
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment