Skip to content

Commit 543538f

Browse files
committed
dealing better with user name - service got aware of it.
1 parent b2e591f commit 543538f

5 files changed

Lines changed: 100 additions & 15 deletions

File tree

app/src/main/java/net/sharksystem/asap/android/Util.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import android.content.Context;
55
import android.util.Log;
66

7+
import net.sharksystem.asap.ASAPException;
8+
9+
import java.io.File;
10+
711
public class Util {
812
public static void unregisterBCR(String logstart, Context context, BroadcastReceiver bcr) {
913
try {
@@ -16,4 +20,36 @@ public static void unregisterBCR(String logstart, Context context, BroadcastRece
1620
+ e.getLocalizedMessage());
1721
}
1822
}
23+
24+
static String makeValidFolderName(CharSequence rootFolder, CharSequence owner) {
25+
Log.d("Util", "create asap folder name is just a dummy.");
26+
return rootFolder + "/userName";
27+
}
28+
29+
public static File getASAPRootDirectory(Context ctx,
30+
CharSequence rootFolder, CharSequence owner) {
31+
32+
File dir = ctx.getExternalFilesDir(Util.makeValidFolderName(rootFolder, owner));
33+
34+
Log.d("Util", "try: " + dir.getAbsolutePath());
35+
try {
36+
dir.mkdirs();
37+
return dir;
38+
}
39+
catch(RuntimeException e) {
40+
Log.d("Util", "cannot create directory: " + dir.getAbsolutePath()
41+
+ " | " + e.getLocalizedMessage() + "try rootFolder only");
42+
}
43+
44+
Log.d("Util", "try: getExternalFilesDir(null)" + dir.getAbsolutePath());
45+
return ctx.getExternalFilesDir(null);
46+
}
47+
48+
public static String getLogStart(Object o) {
49+
return o.getClass().getSimpleName();
50+
}
51+
52+
private String getLogStart() {
53+
return Util.getLogStart(this);
54+
}
1955
}

app/src/main/java/net/sharksystem/asap/android/apps/ASAPActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void sendMessage2Service(Message msg) {
116116
}
117117
}
118118

119-
private String getLogStart() {
119+
protected String getLogStart() {
120120
return this.getClass().getSimpleName();
121121
}
122122

app/src/main/java/net/sharksystem/asap/android/apps/ASAPApplication.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import net.sharksystem.asap.ASAPException;
1212
import net.sharksystem.asap.android.ASAP;
1313
import net.sharksystem.asap.android.ASAPServiceCreationIntent;
14-
import net.sharksystem.asap.android.service.ASAPService;
14+
import net.sharksystem.asap.android.Util;
1515

1616
import java.util.ArrayList;
1717
import java.util.List;
@@ -22,9 +22,10 @@
2222

2323
public class ASAPApplication {
2424
private static final int MY_ASK_FOR_PERMISSIONS_REQUEST = 100;
25-
private final CharSequence asapOwner;
26-
private final CharSequence rootFolder;
27-
private final boolean onlineExchange;
25+
private static ASAPApplication singleton;
26+
private CharSequence asapOwner;
27+
private CharSequence rootFolder;
28+
private boolean onlineExchange;
2829
private boolean initialized = false;
2930

3031
private boolean btDisoverableOn = false;
@@ -38,8 +39,12 @@ public class ASAPApplication {
3839
private List<String> deniedPermissions = new ArrayList<>();
3940
private int activityCount = 0;
4041

41-
protected ASAPApplication(CharSequence asapOwner) {
42-
this(asapOwner, DEFAULT_ROOT_FOLDER_NAME, ASAP.ONLINE_EXCHANGE_DEFAULT);
42+
/**
43+
* setup application by calling getASAPOwner(), getFolderName(), getASAPOnlineExchange().
44+
* Those messagen can and should be overwritten from actual implementations.
45+
*/
46+
protected ASAPApplication() {
47+
this(ASAP.UNKNOWN_USER, DEFAULT_ROOT_FOLDER_NAME, ASAP.ONLINE_EXCHANGE_DEFAULT);
4348
}
4449

4550
protected ASAPApplication(CharSequence asapOwner,
@@ -72,6 +77,11 @@ private void initialize() {
7277
// Intent asapServiceCreationIntent = new Intent(activity, ASAPService.class);
7378
// asapServiceCreationIntent.putExtra(ASAP.USER, this.asapOwner);
7479

80+
// get owner when initializing
81+
this.asapOwner = this.getASAPOwner(this.getActivity());
82+
this.rootFolder = this.getASAPRootFolder(this.getActivity());
83+
this.onlineExchange = this.getASAPOnlineExchange(this.getActivity());
84+
7585
try {
7686
Intent asapServiceCreationIntent = new ASAPServiceCreationIntent(activity,
7787
this.asapOwner, this.rootFolder, this.onlineExchange);
@@ -84,10 +94,51 @@ private void initialize() {
8494
}
8595
}
8696

97+
/**
98+
* could be overwritten
99+
* @param activity
100+
* @return
101+
*/
102+
protected boolean getASAPOnlineExchange(Activity activity) {
103+
return this.onlineExchange;
104+
}
105+
106+
public boolean getASAPOnlineExchange() {
107+
return this.onlineExchange;
108+
}
109+
110+
/**
111+
* could be overwritten
112+
*/
113+
protected CharSequence getASAPRootFolder(Activity activity) {
114+
return this.rootFolder;
115+
}
116+
117+
public CharSequence getASAPRootFolder() {
118+
return Util.getASAPRootDirectory(
119+
this.getActivity(), this.rootFolder, this.asapOwner).getAbsolutePath();
120+
}
121+
122+
/**
123+
* could be overwritten
124+
*/
125+
protected CharSequence getASAPOwner(Activity activity) {
126+
return this.asapOwner;
127+
}
128+
129+
public CharSequence getASAPOwner() {
130+
return this.asapOwner;
131+
}
132+
87133
public static ASAPApplication getASAPApplication() {
88-
return new ASAPApplication("DummyUser");
134+
if(ASAPApplication.singleton == null) {
135+
ASAPApplication.singleton = new ASAPApplication();
136+
}
137+
138+
return ASAPApplication.singleton;
89139
}
90140

141+
91142
public void activityCreated(ASAPActivity asapActivity) {
92143
this.setActivity(asapActivity);
93144
this.initialize();

app/src/main/java/net/sharksystem/asap/android/example/MainActivity.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ else if(view == findViewById(R.id.startDiscovery)) {
7676
}
7777
}
7878

79-
private String getLogStart() {
80-
return "ASAPExampleActivity";
81-
}
82-
8379
@Override
8480
public void asapNotifyBTDiscoverableStopped() {
8581
super.asapNotifyBTDiscoverableStopped();

app/src/main/java/net/sharksystem/asap/android/service/ASAPService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.app.Service;
55
import android.content.Intent;
66
import android.content.pm.PackageManager;
7-
import android.os.Environment;
87
import android.os.IBinder;
98
import android.os.Messenger;
109
import android.support.v4.content.ContextCompat;
@@ -19,6 +18,7 @@
1918
import net.sharksystem.asap.MultiASAPEngineFS_Impl;
2019
import net.sharksystem.asap.android.ASAP;
2120
import net.sharksystem.asap.android.ASAPBroadcastIntent;
21+
import net.sharksystem.asap.android.Util;
2222
import net.sharksystem.asap.android.bluetooth.BluetoothEngine;
2323
import net.sharksystem.asap.android.wifidirect.WifiP2PEngine;
2424

@@ -98,7 +98,7 @@ public void onCreate() {
9898
Log.d(LOGSTART,"onCreate");
9999
}
100100

101-
// comes second - could remove that overwriting method
101+
// comes second - do initializing stuff here
102102
@Override
103103
public int onStartCommand(Intent intent, int flags, int startId) {
104104
Log.d(LOGSTART, "onStartCommand");
@@ -118,7 +118,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
118118

119119
// get root directory
120120
File asapRoot = null;
121-
asapRoot = Environment.getExternalStoragePublicDirectory(this.rootFolder.toString());
121+
//asapRoot = Environment.getExternalStoragePublicDirectory(this.rootFolder.toString());
122+
Log.d(LOGSTART, "use Util.getASAPRootDirectory()");
123+
asapRoot = Util.getASAPRootDirectory(this, this.rootFolder, this.owner);
122124

123125
this.asapEngineRootFolderName = asapRoot.getAbsolutePath();
124126
Log.d(LOGSTART,"work with folder: " + this.asapEngineRootFolderName);

0 commit comments

Comments
 (0)