Pārlūkot izejas kodu

added scrollable console
added another activity for the touchevents

Sebastian Vendt 6 gadi atpakaļ
vecāks
revīzija
0955b8d456

+ 6 - 1
app/src/main/AndroidManifest.xml

@@ -2,6 +2,8 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.motionlogger.sebastianvendt.motionlogger">
 
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
@@ -16,13 +18,16 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-        <activity android:name=".DisplayMessageActivity"
+        <activity
+            android:name=".DisplayMessageActivity"
             android:parentActivityName=".MainActivity">
+
             <!-- The meta-data tag is required if you support API level 15 and lower -->
             <meta-data
                 android:name="android.support.PARENT_ACTIVITY"
                 android:value=".MainActivity" />
         </activity>
+        <activity android:name=".TouchInterface"></activity>
     </application>
 
 </manifest>

+ 3 - 3
app/src/main/java/com/motionlogger/sebastianvendt/motionlogger/DisplayMessageActivity.java

@@ -12,13 +12,13 @@ public class DisplayMessageActivity extends AppCompatActivity {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_display_message);
 
-        // Get the Intent that started this activity and extract the string
+        /*// Get the Intent that started this activity and extract the string
         Intent intent = getIntent();
         String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
-
+        */
         // Capture the layout's TextView and set the string as its text
         TextView textView = findViewById(R.id.textView);
-        textView.setText(message);
+        textView.setText("zero");
 
     }
 }

+ 66 - 23
app/src/main/java/com/motionlogger/sebastianvendt/motionlogger/MainActivity.java

@@ -3,64 +3,107 @@ package com.motionlogger.sebastianvendt.motionlogger;
 import android.content.Context;
 import android.hardware.Sensor;
 import android.hardware.SensorManager;
+import android.os.Environment;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
+import android.text.Layout;
+import android.text.method.ScrollingMovementMethod;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
 import android.widget.EditText;
 import android.content.Intent;
+import android.widget.TextView;
 
 import java.util.Iterator;
 import java.util.List;
 
 
 public class MainActivity extends AppCompatActivity {
-    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
     private SensorManager mSensorManager;
+    private TextView console;
+
+    private View.OnTouchListener handleTouch = new View.OnTouchListener() {
+
+        public boolean onTouch(View v, MotionEvent event) {
+            int x = (int) event.getX();
+            int y = (int) event.getY();
+            Log.d("TAG", "touched" + x + " " + y + " ");
+            return true;
+        }
+    };
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         findViewById(android.R.id.content).setOnTouchListener(handleTouch);
+        console = (TextView) findViewById(R.id.Console);
+        console.setMovementMethod(new ScrollingMovementMethod());
+        //Check the availability of the external storage
+        addMessageToConsole("checking external storage permission\n");
+        if(isExternalStorageWritable()){
+            addMessageToConsole("external storage: write permission available!\n");
+        } else {
+            addMessageToConsole("ERROR: external storage write permission NOT available");
+        }
+
+
+    }
+
+    public boolean isExternalStorageWritable() {
+        String state = Environment.getExternalStorageState();
+        if (Environment.MEDIA_MOUNTED.equals(state)) {
+            return true;
+        }
+        return false;
+    }
 
+    public void readSensors(View view) {
         //list the sensors available on the device
         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
         List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
-
         Iterator<Sensor> sensorIterator = deviceSensors.iterator();
 
         while (sensorIterator.hasNext()){
             Sensor sens = sensorIterator.next();
-            EditText console = (EditText) findViewById(R.id.Console);
-            console.setText(sens.getName());
+            addMessageToConsole(sens.getName() + "\n");
         }
+    }
 
-
-
-
-
+    public void addMessageToConsole(String message) {
+
+       /* if(console != null){
+            console.append(message);
+            final Layout layout = console.getLayout();
+            if(layout != null){
+                int scrollDelta = layout.getLineBottom(console.getLineCount() - 1)
+                        - console.getScrollY() - console.getHeight();
+                if(scrollDelta > 0)
+                    console.scrollBy(0, scrollDelta);
+            }
+        }*/
+
+        console.append(message);/*
+        Layout consoleLayout = console.getLayout();
+        if(consoleLayout != null) {
+            final int scrollAmount = consoleLayout.getLineTop(console.getLineCount()) - console.getHeight();
+            if(scrollAmount > 0) {
+                console.scrollTo(0, scrollAmount);
+            } else {
+                console.scrollTo(0,0);
+            }
+        }*/
     }
 
     public void sendMessage(View view) {
-        Intent intent = new Intent(this, DisplayMessageActivity.class);
-        EditText editText = (EditText) findViewById(R.id.editText);
+       /* Intent intent = new Intent(this, DisplayMessageActivity.class);
+        EditText editText = (EditText) findViewById(R.id.Console);
         String message = editText.getText().toString();
         intent.putExtra(EXTRA_MESSAGE, message);
-        startActivity(intent);
+        startActivity(intent);*/
 
     }
 
 
-}
-
-
-private View.OnTouchListener handleTouch = new View.OnTouchListener() {
-
-    public boolean onTouch(View v, MotionEvent event) {
-        int x = (int) event.getX();
-        int y = (int) event.getY();
-        Log.d("TAG", "touched" + x + " " + y + " ");
-        return true;
-    }
-};
+};

+ 13 - 0
app/src/main/java/com/motionlogger/sebastianvendt/motionlogger/TouchInterface.java

@@ -0,0 +1,13 @@
+package com.motionlogger.sebastianvendt.motionlogger;
+
+import android.support.v7.app.AppCompatActivity;
+import android.os.Bundle;
+
+public class TouchInterface extends AppCompatActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_touch_interface);
+    }
+}

+ 26 - 10
app/src/main/res/layout/activity_main.xml

@@ -18,19 +18,35 @@
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent" />
 
-    <EditText
+    <Button
+        android:id="@+id/readSensors"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="8dp"
+        android:layout_marginEnd="24dp"
+        android:layout_marginRight="24dp"
+        android:onClick="readSensors"
+        android:text="@string/button_sensor"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/Console" />
+
+    <TextView
         android:id="@+id/Console"
-        android:layout_width="334dp"
-        android:layout_height="194dp"
-        android:layout_marginStart="16dp"
-        android:layout_marginLeft="16dp"
+        android:layout_width="358dp"
+        android:layout_height="185dp"
+        android:layout_marginStart="8dp"
+        android:layout_marginLeft="8dp"
         android:layout_marginTop="8dp"
-        android:layout_marginEnd="16dp"
-        android:layout_marginRight="16dp"
-        android:ems="10"
-        android:inputType="textPersonName"
-        android:text="@string/Console_text"
+        android:layout_marginEnd="8dp"
+        android:layout_marginRight="8dp"
+        android:cursorVisible="false"
+        android:gravity="bottom"
+        android:scrollbars="vertical"
+        android:singleLine="false"
+        android:text="@string/console_text"
+        android:textSize="10sp"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
+
 </android.support.constraint.ConstraintLayout>

+ 18 - 0
app/src/main/res/layout/activity_touch_interface.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".TouchInterface">
+
+    <ImageView
+        android:id="@+id/keyboardLayout"
+        android:layout_width="385dp"
+        android:layout_height="322dp"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintHorizontal_bias="0.0"
+        app:layout_constraintStart_toStartOf="parent"
+        tools:srcCompat="@drawable/googlekeyboardlayout2" />
+</android.support.constraint.ConstraintLayout>

+ 2 - 1
app/src/main/res/values/strings.xml

@@ -2,5 +2,6 @@
     <string name="app_name">MotionLogger</string>
     <string name="edit_message">test</string>
     <string name="button_start">Start</string>
-    <string name="Console_text">Console</string>
+    <string name="console_text">Console\n1.1\n</string>
+    <string name="button_sensor">read Sensors</string>
 </resources>