66import android .content .Context ;
77import android .content .Intent ;
88import android .content .pm .PackageManager ;
9+ import android .support .annotation .CallSuper ;
910import android .support .v4 .app .ActivityCompat ;
1011import android .support .v4 .content .ContextCompat ;
1112import android .util .Log ;
3738public class ASAPApplication extends BroadcastReceiver {
3839 private static final int MY_ASK_FOR_PERMISSIONS_REQUEST = 100 ;
3940 private static ASAPApplication singleton ;
40- private final Collection <CharSequence > supportedFormats ;
41- private CharSequence asapOwner ;
41+ private Collection <CharSequence > supportedFormats ;
42+ private CharSequence asapOwner = null ;
4243 private CharSequence rootFolder ;
4344 private boolean onlineExchange ;
4445 private boolean initialized = false ;
@@ -47,7 +48,7 @@ public class ASAPApplication extends BroadcastReceiver {
4748 private boolean btDisoveryOn = false ;
4849 private boolean btEnvironmentOn = false ;
4950
50- private ASAPActivity activity ;
51+ private Activity activity ;
5152
5253 private List <String > requiredPermissions ;
5354 private List <String > grantedPermissions = new ArrayList <>();
@@ -60,9 +61,9 @@ public class ASAPApplication extends BroadcastReceiver {
6061 * @param supportedFormats ensure that asap engines using that formats are present -
6162 * create if necessary.
6263 */
63- protected ASAPApplication (Collection <CharSequence > supportedFormats ) {
64+ protected ASAPApplication (Collection <CharSequence > supportedFormats , Activity initialActivity ) {
6465 this (supportedFormats , ASAPAndroid .UNKNOWN_USER , DEFAULT_ROOT_FOLDER_NAME ,
65- ASAPAndroid .ONLINE_EXCHANGE_DEFAULT );
66+ ASAPAndroid .ONLINE_EXCHANGE_DEFAULT , initialActivity );
6667 }
6768
6869 int getNumberASAPActivities () {
@@ -73,43 +74,62 @@ int getNumberASAPActivities() {
7374 * setup application without parameter. Use default for owner, root folder for asap storage
7475 * and online exchange behaviour. Don't setup any asap engine - take engines which are
7576 * already present when starting up.
76- */
77- protected ASAPApplication () {
77+ protected ASAPApplication(Activity initialActivity) {
7878 this(null, ASAPAndroid.UNKNOWN_USER, DEFAULT_ROOT_FOLDER_NAME,
7979 ASAPAndroid.ONLINE_EXCHANGE_DEFAULT);
8080 }
81+ */
8182
8283 protected ASAPApplication (Collection <CharSequence > supportedFormats ,
8384 CharSequence asapOwner ,
8485 CharSequence rootFolder ,
85- boolean onlineExchange ) {
86+ boolean onlineExchange ,
87+ Activity initialActivity ) {
8688
8789 this .supportedFormats = supportedFormats ;
8890 this .asapOwner = asapOwner ;
8991 this .rootFolder = rootFolder ;
9092 this .onlineExchange = onlineExchange ;
93+
94+ // set context
95+ this .setActivity (initialActivity );
96+
97+ Log .d (this .getLogStart (), "initialize ASAP Application user side" );
98+ // required permissions
99+ this .requiredPermissions = new ArrayList <>();
100+ this .requiredPermissions .add (Manifest .permission .WRITE_EXTERNAL_STORAGE );
101+ this .requiredPermissions .add (Manifest .permission .ACCESS_WIFI_STATE );
102+ this .requiredPermissions .add (Manifest .permission .CHANGE_WIFI_STATE );
103+ this .requiredPermissions .add (Manifest .permission .CHANGE_NETWORK_STATE );
104+ this .requiredPermissions .add (Manifest .permission .INTERNET );
105+ this .requiredPermissions .add (Manifest .permission .BLUETOOTH );
106+ this .requiredPermissions .add (Manifest .permission .BLUETOOTH_ADMIN );
107+ this .requiredPermissions .add (Manifest .permission .ACCESS_COARSE_LOCATION );
108+
109+ // check for write permissions
110+ Log .d (this .getLogStart (), "ask for required permissions" );
111+ this .askForPermissions ();
91112 }
92113
93- private void initialize () {
114+ @ CallSuper
115+ public void startASAPApplication () {
94116 if (!this .initialized ) {
95- Log .d (this .getLogStart (), "initialize ASAP Application user side" );
96-
97- // required permissions
98- this .requiredPermissions = new ArrayList <>();
99- this .requiredPermissions .add (Manifest .permission .WRITE_EXTERNAL_STORAGE );
100- this .requiredPermissions .add (Manifest .permission .ACCESS_WIFI_STATE );
101- this .requiredPermissions .add (Manifest .permission .CHANGE_WIFI_STATE );
102- this .requiredPermissions .add (Manifest .permission .CHANGE_NETWORK_STATE );
103- this .requiredPermissions .add (Manifest .permission .INTERNET );
104- this .requiredPermissions .add (Manifest .permission .BLUETOOTH );
105- this .requiredPermissions .add (Manifest .permission .BLUETOOTH_ADMIN );
106- this .requiredPermissions .add (Manifest .permission .ACCESS_COARSE_LOCATION );
107-
108- // check for write permissions
109- this .askForPermissions ();
110-
111- // get owner when initializing
112- this .asapOwner = this .getASAPOwnerID ();
117+ Log .d (this .getLogStart (), "initialize and launch ASAP Service" );
118+ // collect parameters
119+ if (this .asapOwner == null || this .asapOwner .equals (ASAPAndroid .UNKNOWN_USER )) {
120+ Log .d (this .getLogStart (), "asapOwnerID not set at all or set to default - call getASAPOwnerID" );
121+ this .asapOwner = this .getOwnerID ();
122+ } else {
123+ Log .d (this .getLogStart (), "owner already set" );
124+ }
125+
126+ if (this .supportedFormats == null || this .supportedFormats .size () < 1 ) {
127+ Log .d (this .getLogStart (), "supportedFormats null or empty - call getSupportedFormats()" );
128+ this .supportedFormats = this .getSupportFormats ();
129+ } else {
130+ Log .d (this .getLogStart (), "supportedFormats already set" );
131+ }
132+
113133 //this.rootFolder = this.getASAPRootFolder(); // service creates absolute path
114134 this .onlineExchange = this .getASAPOnlineExchange ();
115135
@@ -125,6 +145,8 @@ private void initialize() {
125145 } catch (ASAPException e ) {
126146 Log .e (this .getLogStart (), "could not start ASAP Service - fatal" );
127147 }
148+ } else {
149+ Log .e (this .getLogStart (), "try to re-start application - not allowed. Ignored" );
128150 }
129151 }
130152
@@ -153,48 +175,52 @@ protected CharSequence getASAPRootFolder(Activity activity) {
153175
154176 public CharSequence getASAPRootFolder () {
155177 return Util .getASAPRootDirectory (
156- this .getActivity (), this .rootFolder , this .asapOwner ).getAbsolutePath ();
178+ this .getActivity (), this .rootFolder , this .getOwnerID ()).getAbsolutePath ();
179+ }
180+
181+ public CharSequence getASAPComponentFolder (CharSequence format ) {
182+ return this .getASAPRootFolder () +"/" + format ;
157183 }
158184
159185 /**
160186 * could be overwritten
161187 */
162- public CharSequence getASAPOwnerID () {
188+ public CharSequence getOwnerID () {
163189 return this .asapOwner ;
164190 }
165191
192+ public CharSequence getOwnerName () {
193+ return ASAPEngineFS .ANONYMOUS_OWNER ;
194+ }
195+
166196 public Collection <CharSequence > getSupportFormats () {
167197 return this .supportedFormats ;
168198 }
169199
170200 public static ASAPApplication getASAPApplication () {
171201 if (ASAPApplication .singleton == null ) {
172- ASAPApplication . singleton = new ASAPApplication ( );
202+ throw new ASAPComponentNotYetInitializedException ( "ASAP Application not yet initialized" );
173203 }
174204
175205 return ASAPApplication .singleton ;
176206 }
177207
178- protected void setASAPApplication (ASAPApplication asapApplication ) {
179- ASAPApplication .singleton = asapApplication ;
180- }
181-
182- public static ASAPApplication getASAPApplication (Collection <CharSequence > supportedFormats ) {
208+ public static ASAPApplication initializeASAPApplication (
209+ Collection <CharSequence > supportedFormats , Activity initialActivity ) {
183210 if (ASAPApplication .singleton == null ) {
184- ASAPApplication .singleton = new ASAPApplication (supportedFormats );
211+ ASAPApplication .singleton = new ASAPApplication (supportedFormats , initialActivity );
212+ } else {
213+ Log .e (ASAPApplication .class .getSimpleName (),
214+ "tried to initialized already initialized application - ignored." );
185215 }
186216
187217 return ASAPApplication .singleton ;
188218 }
189219
190- public static ASAPApplication getASAPApplication (CharSequence supportedFormat ) {
191- if (ASAPApplication .singleton == null ) {
192- Collection <CharSequence > formats = new HashSet <>();
193- formats .add (supportedFormat );
194- ASAPApplication .singleton = new ASAPApplication (formats );
195- }
196-
197- return ASAPApplication .singleton ;
220+ public static ASAPApplication initializeASAPApplication (
221+ CharSequence supportedFormat , Activity initialActivity ) {
222+ Collection <CharSequence > formats = new HashSet <>();
223+ return ASAPApplication .initializeASAPApplication (formats , initialActivity );
198224 }
199225
200226 public String getApplicationRootFolder (String appName ) {
@@ -206,26 +232,30 @@ public String getApplicationRootFolder(String appName) {
206232 return absoluteASAPApplicationRootFolder ;
207233 }
208234
209- public void activityCreated (ASAPActivity asapActivity , boolean initASAPApplication ) {
235+ @ CallSuper
236+ public void activityCreated (ASAPActivity asapActivity ) {
210237 this .setActivity (asapActivity );
211- if (initASAPApplication ) this .initialize ();
238+ /* was to tricky barely understandable
239+ if(initASAPApplication) this.startASAPApplication();
240+ */
212241
213242 this .activityASAPActivities ++;
214243 Log .d (this .getLogStart (), "activity created. New activity count == "
215244 + this .activityASAPActivities );
216245 }
217246
247+ @ CallSuper
218248 public void activityDestroyed (ASAPActivity asapActivity ) {
219249 this .activityASAPActivities --;
220250 Log .d (this .getLogStart (), "activity destroyed. New activity count == "
221251 + this .activityASAPActivities );
222252 }
223253
224- public ASAPActivity getActivity () {
254+ public Activity getActivity () {
225255 return this .activity ;
226256 }
227257
228- void setActivity (ASAPActivity activity ) {
258+ void setActivity (Activity activity ) {
229259 Log .d (this .getLogStart (), "activity set" );
230260 this .activity = activity ;
231261 }
@@ -239,7 +269,7 @@ private void askForPermissions() {
239269 String wantedPermission = requiredPermissions .remove (0 );
240270 Log .d (this .getLogStart (), "handle permission " + wantedPermission );
241271
242- if (ContextCompat .checkSelfPermission (this .activity , wantedPermission )
272+ if (ContextCompat .checkSelfPermission (this .getActivity () , wantedPermission )
243273 == PackageManager .PERMISSION_GRANTED ) {
244274 // already granted
245275 Log .d (this .getLogStart (), wantedPermission + " already granted" );
@@ -464,6 +494,10 @@ public void setOnlinePeersList(List<CharSequence> peerList) {
464494 this .onlinePeerList = peerList ;
465495 }
466496
497+ public void setupDrawerLayout (Activity activity ) {
498+ Log .d (this .getLogStart (), "setupDrawerLayout dummy called: could be overwritten if needed. Don't do anything here" );
499+ }
500+
467501 private String getLogStart () {
468502// int objectID = this.hashCode();
469503// return "ASAPApplication(" + objectID + ")";
0 commit comments