-
Notifications
You must be signed in to change notification settings - Fork 1
Initialize
You implemented an application with ASAP/Shark. Your application needs an object reference to a ASAPeer or even better a SharkPeer. That’s a mandatory initialization step in every Android ASAP application. This step is required during any application launch.
Applications can also write permanent information during first initialization. You application might ask users to give themselves a name, an id or something like that. Those data can be stored with shared preferences, a database or something else.
We have two kind of initialization.
- during application installation
- during each application launch
You implemented an application with ASAP/Shark. Your application needs an object reference to a ASAPeer or even better a SharkPeer. That’s a mandatory initialization step in every Android ASAP application. This step is required during any application launch.
Applications can also write permanent information during first initialization. You application might ask users to give themselves a name, an id or something like that. Those data can be stored with shared preferences, a database or something else.
We have two kind of initialization.
Launching is system is often called bootstrepping. Those processes have the tendency to become pretty weird. You know what I mean if you ever had a look into launching code of an operating system. The following will not be that mind-blowing.
Here is the point: There is a special Activity in each Android application. It is the first Activity that is instantiated during application launch. It is defined in the manifest, like this.
<activity android:name=".sharknet.android.InitActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here can be a challenge. Does your application needs persistent data like a user name/id or something else. This activity needs to check if those data are already set. The next, the real first activity can be started. If not, this application is not yet initialized. It was most probably launched the very first time.
(Complete implementation can be found here).
public class InitActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
/* get persistent data e.g. from shared preference
* - throw execption if not present yet
*/
String ownerID = ...;
// data found - app was initialized before
// init your system
this.initializeSystem(ownerID);
// launch 'real' first activity
Class realFirstActivity = YourAppsFirstActivity.class;
this.finish();
Intent intent = new Intent(this, realFirstActivity);
this.startActivity(intent);
}
catch(YourException se) {
setContentView(R.layout.init);
// this init activity proceeds
}
}
This algorithm is simple. We try to get some persistent information (ownerID
). They are needed to initialize our system. We assume a method that get those data from a persistent memory like share preference. We also assume that this method throws an Exception an we and up in the catch
clause.
We just set a layout (R.layout.init
). The activity remains active. (We ask for a user name and store it in shared preferences.)
We understand this initial activity as a kind of guard. It checks if this application was properly initialized. It retrieves general information and launches an first activity that works on an initialized system.
Feel free to use this code a template for your own initial activity.
We assume you implemented a SharkComponent. There can be an arbitrary number of components in each application but all work with a single SharkPeer. A SharkPeer wraps an ASAPPeer. There is no need to go into details here.
You also already know that we use file system to store data. It works in Android as well as any other Java environment. Each SharkPeer needs a root folder for its construction.
The following code is from class SharkNetApp
String ownerID; // got as parameter, see above
File rootDir =
Util.getASAPRootDirectory(initialActivity, SharkNetApp.APP_FOLDER_NAME, ownerID);
// produce application side shark peer
SharkNetApp.singleton.sharkPeer = new SharkPeerFS(
ownerID,
rootDir.getAbsolutePath()
);
It is somewhat tricky to get a valid folder name for an application in Android. This library provides this Util.getASAPRootDirectory
method to produce on. Do not forget to get the absolute path as parameter for the SharkPeerFS constructor.
We have got a SharkPeer instance. We can add our components. You already familiar with this procedure. You implemented a factory. This factory can require a previously created component. Here is example code derived from code in our SharkNetApp class.
SharkPeer sharkPeer = ...; // see above
YourComponent1Factory yourFactory1 =
new YourComponent1Factory(/* requires no other component */);
// register this component with shark peer
sharkPeer.addComponent(yourFactory1, YourComponent1.class);
// get object reference
YourComponent1 yourComponent1 =
(YourComponent1)sharkPeer.getComponent(YourComponent1.class);
// another component - needs component1
YourComponent2Factory yourFactory2 =
new YourComponent2Factory(yourComponent1);
// register this component with shark peer
sharkPeer.addComponent(yourFactory2, YourComponent2.class);
There are two components in this example. Component 2 needs an instance of component 1. We create a factory of component 1 first. We add it to our SharkPeer object. Now, we get a object reference to component 1 (sharkPeer.getComponent
). This object reference becomes parameter for your component factory number 2. It is added to our SharkPeer as well.
You already know this code. This code has no Android specifics. This code run in a pure Java project and in Android without any change. SharkPeer creation was different. Rest is identical.
You components are registered with SharkPeer. We can launch the system.
SharkPeer sharkPeer = ...; // see above
String ownerID == ...; // see above
Activity initialActivity = ...; // see above
// init ASAPPeer
ASAPAndroidPeer.initializePeer(
ownerID,
sharkPeer.getFormats(),
SharkNetApp.APP_FOLDER_NAME,
initialActivity);
// launch service side
ASAPAndroidPeer applicationSideASAPPeer = ASAPAndroidPeer.startPeer(initialActivity);
// use asap peer proxy for this app side shark peer
sharkPeer.start(applicationSideASAPPeer);
We already got three object references: sharkPeer
was created as instance of SharkPeerFS. We got an ownerID
from a storage. This value was set during application installation. This code is assumed to be called by our initialActivity
, see above.
We have to initialize the ASAPAndroidPeer with those three parameters and a application folder name of you choice. This constant was already used when we created the root folder for our SharkPeer. A lot of things happen during this init process. Have a look into section insides if you are interest. A do not have to deal with details.
ASAPAndroidPeer inherits from ASAPPeer. We get an object reference by calling a ASAPAndroidPeer.startPeer(initialActivity)
. Finally, we can launch our SharkPeer. System is up an running. You only need to implement a GUI.
You can get a object reference of each of your components always by calling sharkPeer.getComponent().