import android.view.MenuItem;\r
import android.widget.LinearLayout;\r
import android.widget.TextView;\r
+import android.content.SharedPreferences;\r
+import android.content.res.AssetManager;\r
+import android.preference.PreferenceManager;\r
\r
import org.iotivity.base.ModeType;\r
import org.iotivity.base.OcPlatform;\r
import org.iotivity.base.QualityOfService;\r
import org.iotivity.base.ServiceType;\r
\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+\r
import base.iotivity.org.examples.message.IMessageLogger;\r
\r
/**\r
\r
public class SimpleServer extends Activity implements IMessageLogger {\r
private final static String TAG = "SimpleServer: ";\r
+ private static final int BUFFER_SIZE = 1024;
+ private String filePath = "";\r
private TextView mEventsTextView;\r
private MessageReceiver mMessageReceiver = new MessageReceiver();\r
\r
OcRepresentation rep = new OcRepresentation();\r
rep.setValueBool("test", false);\r
boolean result = rep.getValueBool("test");\r
+ filePath = getFilesDir().getPath() + "/";// data/data/<package>/files/\r
+ //copy json when application runs first time\r
+ SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);\r
+ boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);\r
+ if (isFirstRun) {\r
+ copyJsonFromAsset();\r
+ SharedPreferences.Editor editor = wmbPreference.edit();\r
+ editor.putBoolean("FIRSTRUN", false);\r
+ editor.commit();\r
+ }
\r
initOICStack();\r
}\r
+
+ /**\r
+ * Copy svr db json file from assets folder to app data files dir\r
+ */\r
+ private void copyJsonFromAsset() {\r
+ AssetManager assetManager = getAssets();\r
+\r
+ InputStream in = null;\r
+ OutputStream out = null;\r
+ try {\r
+\r
+ in = assetManager.open(StringConstants.OIC_SERVER_JSON_DB_FILE);\r
+ File file = new File(filePath);\r
+ //check files directory exists\r
+ if (!(file.exists() && file.isDirectory())) {\r
+ file.mkdirs();\r
+ }\r
+ out = new FileOutputStream(filePath + StringConstants.OIC_SERVER_JSON_DB_FILE);\r
+ copyFile(in, out);\r
+ } catch (NullPointerException e) {\r
+ logMessage(TAG + "Null pointer exception " + e.getMessage());\r
+ Log.e(TAG, e.getMessage());\r
+ } catch (FileNotFoundException e) {\r
+ logMessage(TAG + "Json svr db file not found " + e.getMessage());\r
+ Log.e(TAG, e.getMessage());\r
+ } catch (IOException e) {\r
+ logMessage(TAG + StringConstants.OIC_SERVER_JSON_DB_FILE + " file copy failed");\r
+ Log.e(TAG, e.getMessage());\r
+ } finally {\r
+ if (in != null) {\r
+ try {\r
+ in.close();\r
+ } catch (IOException e) {\r
+ Log.e(TAG, e.getMessage());\r
+ }\r
+ }\r
+ if (out != null) {\r
+ try {\r
+ out.close();\r
+ } catch (IOException e) {\r
+ Log.e(TAG, e.getMessage());\r
+ }\r
+ }\r
+ }\r
+ }\r
\r
+ private void copyFile(InputStream in, OutputStream out) throws IOException {\r
+ byte[] buffer = new byte[BUFFER_SIZE];\r
+ int read;\r
+ while ((read = in.read(buffer)) != -1) {\r
+ out.write(buffer, 0, read);\r
+ }\r
+ }\r
/**\r
* configure OIC platform and call findResource\r
*/\r
ModeType.SERVER,\r
"0.0.0.0", // bind to all available interfaces\r
0,\r
- QualityOfService.LOW);\r
+ QualityOfService.LOW,\r
+ filePath + StringConstants.OIC_SERVER_JSON_DB_FILE);\r
OcPlatform.Configure(cfg);\r
// Create instance of lightResource\r
LightResource myLight = new LightResource(this);\r
// create and register a resource\r
myLight.createResource0();\r
}\r
-\r
+
public class MessageReceiver extends BroadcastReceiver {\r
@Override\r
public void onReceive(Context context, Intent intent) {\r