Clean up some SonarQube warnings (trailing whitespace, etc).
[platform/upstream/iotivity.git] / android / examples / simpleserver / src / main / java / org / iotivity / base / examples / simpleserver / SimpleServer.java
index b48a9f5..f929ea0 100644 (file)
-/*\r
- * //******************************************************************\r
- * //\r
- * // Copyright 2015 Intel Corporation.\r
- * //\r
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
- * //\r
- * // Licensed under the Apache License, Version 2.0 (the "License");\r
- * // you may not use this file except in compliance with the License.\r
- * // You may obtain a copy of the License at\r
- * //\r
- * //      http://www.apache.org/licenses/LICENSE-2.0\r
- * //\r
- * // Unless required by applicable law or agreed to in writing, software\r
- * // distributed under the License is distributed on an "AS IS" BASIS,\r
- * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * // See the License for the specific language governing permissions and\r
- * // limitations under the License.\r
- * //\r
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
- */\r
-\r
-package org.iotivity.base.examples.simpleserver;\r
-\r
-import android.app.Activity;\r
-import android.content.BroadcastReceiver;\r
-import android.content.Context;\r
-import android.content.Intent;\r
-import android.content.IntentFilter;\r
-import android.os.Bundle;\r
-import android.os.Message;\r
-import android.support.v4.content.LocalBroadcastManager;\r
-import android.text.method.ScrollingMovementMethod;\r
-import android.util.Log;\r
-import android.view.Menu;\r
-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.OcRepresentation;\r
-import org.iotivity.base.PlatformConfig;\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
- * SimpleServer\r
- *\r
- * SimpleServer instantiates a TextView and creates and configures OICPlatform.\r
- * It also creates a LightResource and waits for the incoming client requests to handle specific scenarios.\r
- * This implements IMessageLogger to display messages on the screen\r
- */\r
-\r
-public class SimpleServer extends Activity implements IMessageLogger {\r
-    private final static String TAG = "SimpleServer: ";\r
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2015 Intel Corporation.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+package org.iotivity.base.examples.simpleserver;
+
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Bundle;
+import android.os.Message;
+import android.support.v4.content.LocalBroadcastManager;
+import android.text.method.ScrollingMovementMethod;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+import android.content.SharedPreferences;
+import android.content.res.AssetManager;
+import android.preference.PreferenceManager;
+
+import org.iotivity.base.ModeType;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.base.PlatformConfig;
+import org.iotivity.base.QualityOfService;
+import org.iotivity.base.ServiceType;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import base.iotivity.org.examples.message.IMessageLogger;
+
+/**
+ * SimpleServer
+ *
+ * SimpleServer instantiates a TextView and creates and configures OICPlatform.
+ * It also creates a LightResource and waits for the incoming client requests to handle specific scenarios.
+ * This implements IMessageLogger to display messages on the screen
+ */
+
+public class SimpleServer extends Activity implements IMessageLogger {
+    private final static String TAG = "SimpleServer: ";
     private static final int BUFFER_SIZE = 1024;
-    private String filePath = "";\r
-    private TextView mEventsTextView;\r
-    private MessageReceiver mMessageReceiver = new MessageReceiver();\r
-\r
-    @Override\r
-    protected void onCreate(Bundle savedInstanceState) {\r
-        super.onCreate(savedInstanceState);\r
-        setContentView(R.layout.activity_main);\r
-        registerReceiver(mMessageReceiver, new IntentFilter("org.iotivity.base.examples.simpleserver"));\r
-\r
-        mEventsTextView = new TextView(this);\r
-        mEventsTextView.setMovementMethod(new ScrollingMovementMethod());\r
-        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);\r
-        layout.addView(mEventsTextView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f));\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
+    private String filePath = "";
+    private TextView mEventsTextView;
+    private MessageReceiver mMessageReceiver = new MessageReceiver();
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+        registerReceiver(mMessageReceiver, new IntentFilter("org.iotivity.base.examples.simpleserver"));
+
+        mEventsTextView = new TextView(this);
+        mEventsTextView.setMovementMethod(new ScrollingMovementMethod());
+        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
+        layout.addView(mEventsTextView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f));
+
+        OcRepresentation rep = new OcRepresentation();
+        rep.setValueBool("test", false);
+        boolean result = rep.getValueBool("test");
+        filePath = getFilesDir().getPath() + "/";//  data/data/<package>/files/
+        //copy json when application runs first time
+        SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
+        boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
+        if (isFirstRun) {
+            copyJsonFromAsset();
+            SharedPreferences.Editor editor = wmbPreference.edit();
+            editor.putBoolean("FIRSTRUN", false);
+            editor.commit();
         }
-\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
-    private void initOICStack() {\r
-        //create platform config\r
-        PlatformConfig cfg = new PlatformConfig(\r
-                this,\r
-                ServiceType.IN_PROC,\r
-                ModeType.SERVER,\r
-                "0.0.0.0", // bind to all available interfaces\r
-                0,\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
-
-    public class MessageReceiver extends BroadcastReceiver {\r
-        @Override\r
-        public void onReceive(Context context, Intent intent) {\r
-            final String message = intent.getStringExtra(StringConstants.MESSAGE);\r
-            logMessage(message);\r
-        }\r
-    }\r
-\r
-    public void logMessage(final String text) {\r
-        runOnUiThread(new Runnable() {\r
-            public void run() {\r
-                final Message msg = new Message();\r
-                msg.obj = text;\r
-                mEventsTextView.append("\n");\r
-                mEventsTextView.append(text);\r
-            }\r
-        });\r
-        Log.i(TAG, text);\r
-    }\r
-\r
-    @Override\r
-    public boolean onCreateOptionsMenu(Menu menu) {\r
-        getMenuInflater().inflate(R.menu.menu_main, menu);\r
-        return true;\r
-    }\r
-\r
-    @Override\r
-    public boolean onOptionsItemSelected(MenuItem item) {\r
-        int id = item.getItemId();\r
-        if (id == R.id.action_settings) {\r
-            return true;\r
-        }\r
-        return super.onOptionsItemSelected(item);\r
-    }\r
-\r
-    @Override\r
-    public void onDestroy() {\r
-        super.onDestroy();\r
-        onStop();\r
-    }\r
-\r
-    @Override\r
-    protected void onStop() {\r
-        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);\r
-        super.onStop();\r
-    }\r
+
+        initOICStack();
+    }
+
+    /**
+     * Copy svr db json file from assets folder to app data files dir
+     */
+    private void copyJsonFromAsset() {
+        AssetManager assetManager = getAssets();
+
+        InputStream in = null;
+        OutputStream out = null;
+        try {
+
+            in = assetManager.open(StringConstants.OIC_SERVER_JSON_DB_FILE);
+            File file = new File(filePath);
+            //check files directory exists
+            if (!(file.exists() && file.isDirectory())) {
+                file.mkdirs();
+            }
+            out = new FileOutputStream(filePath + StringConstants.OIC_SERVER_JSON_DB_FILE);
+            copyFile(in, out);
+        } catch (NullPointerException e) {
+            logMessage(TAG + "Null pointer exception " + e.getMessage());
+            Log.e(TAG, e.getMessage());
+        } catch (FileNotFoundException e) {
+            logMessage(TAG + "Json svr db file not found " + e.getMessage());
+            Log.e(TAG, e.getMessage());
+        } catch (IOException e) {
+            logMessage(TAG + StringConstants.OIC_SERVER_JSON_DB_FILE + " file copy failed");
+            Log.e(TAG, e.getMessage());
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    Log.e(TAG, e.getMessage());
+                }
+            }
+            if (out != null) {
+                try {
+                    out.close();
+                } catch (IOException e) {
+                    Log.e(TAG, e.getMessage());
+                }
+            }
+        }
+    }
+
+    private void copyFile(InputStream in, OutputStream out) throws IOException {
+        byte[] buffer = new byte[BUFFER_SIZE];
+        int read;
+        while ((read = in.read(buffer)) != -1) {
+            out.write(buffer, 0, read);
+        }
+    }
+    /**
+     * configure OIC platform and call findResource
+     */
+    private void initOICStack() {
+        //create platform config
+        PlatformConfig cfg = new PlatformConfig(
+                this,
+                ServiceType.IN_PROC,
+                ModeType.SERVER,
+                "0.0.0.0", // bind to all available interfaces
+                0,
+                QualityOfService.LOW,
+                filePath + StringConstants.OIC_SERVER_JSON_DB_FILE);
+        OcPlatform.Configure(cfg);
+        // Create instance of lightResource
+        LightResource myLight = new LightResource(this);
+        // create and register a resource
+        myLight.createResource0();
+    }
+
+    public class MessageReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            final String message = intent.getStringExtra(StringConstants.MESSAGE);
+            logMessage(message);
+        }
+    }
+
+    public void logMessage(final String text) {
+        runOnUiThread(new Runnable() {
+            public void run() {
+                final Message msg = new Message();
+                msg.obj = text;
+                mEventsTextView.append("\n");
+                mEventsTextView.append(text);
+            }
+        });
+        Log.i(TAG, text);
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        getMenuInflater().inflate(R.menu.menu_main, menu);
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        int id = item.getItemId();
+        if (id == R.id.action_settings) {
+            return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        onStop();
+    }
+
+    @Override
+    protected void onStop() {
+        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
+        super.onStop();
+    }
 }
\ No newline at end of file