Added FirstStartActivity.

This commit is contained in:
Felix Ableitner 2015-06-24 15:25:22 +02:00
parent 58da843d9b
commit 584d176d73
5 changed files with 140 additions and 15 deletions

View file

@ -17,16 +17,20 @@
android:theme="@style/AppTheme" >
<activity
android:name=".activities.MainActivity"
android:name=".activities.FirstStartActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name" />
<activity
android:name=".activities.AddContactsActivity"
android:label="@string/add_contacts" >

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enter_name"
android:labelFor="@+id/username"
android:layout_above="@+id/username"
android:layout_centerHorizontal="true"
style="@style/Base.TextAppearance.AppCompat.Large"/>
<EditText
android:id="@+id/username"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textCapWords"
android:singleLine="true"
android:layout_centerInParent="true"
android:gravity="center"
android:selectAllOnFocus="true"/>
<Button
android:id="@+id/done"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/done"
android:layout_alignParentBottom="true"
style="?android:attr/buttonBarButtonStyle"/>
</RelativeLayout>

View file

@ -7,6 +7,18 @@
<string name="app_name">EnsiChat</string>
<!-- FirstStartActivity -->
<!-- Activity title -->
<string name="welcome">Welcome!</string>
<!-- Label for name field -->
<string name="enter_name">Enter your Name:</string>
<!-- Label for button to finish activity -->
<string name="done">Done</string>
<!-- MainActivity -->
<!-- Toast shown if user denies request to enable bluetooth -->

View file

@ -0,0 +1,82 @@
package com.nutomic.ensichat.activities
import android.bluetooth.BluetoothAdapter
import android.content.{Context, Intent}
import android.os.Bundle
import android.preference.PreferenceManager
import android.support.v7.app.AppCompatActivity
import android.view.View.OnClickListener
import android.view.inputmethod.{EditorInfo, InputMethodManager}
import android.view.{KeyEvent, View}
import android.widget.TextView.OnEditorActionListener
import android.widget.{Button, EditText, TextView}
import com.nutomic.ensichat.R
import com.nutomic.ensichat.fragments.SettingsFragment
/**
* Shown on first start, lets the user enter their name.
*/
class FirstStartActivity extends AppCompatActivity with OnEditorActionListener with OnClickListener {
private val KeyIsFirstStart = "first_start"
private lazy val preferences = PreferenceManager.getDefaultSharedPreferences(this)
private lazy val imm = getSystemService(Context.INPUT_METHOD_SERVICE)
.asInstanceOf[InputMethodManager]
private lazy val username = findViewById(R.id.username).asInstanceOf[EditText]
private lazy val done = findViewById(R.id.done) .asInstanceOf[Button]
override def onCreate(savedInstanceState: Bundle): Unit = {
super.onCreate(savedInstanceState)
if (!preferences.getBoolean(KeyIsFirstStart, true)) {
startMainActivity()
return
}
setContentView(R.layout.activity_first_start)
setTitle(R.string.welcome)
username.setText(BluetoothAdapter.getDefaultAdapter.getName)
username.setOnEditorActionListener(this)
done.setOnClickListener(this)
imm.showSoftInput(username, InputMethodManager.SHOW_IMPLICIT)
}
/**
* Calls [[save]] on enter click.
*/
override def onEditorAction(v: TextView, actionId: Int, event: KeyEvent): Boolean = {
if (actionId == EditorInfo.IME_ACTION_DONE) {
save()
true
}
else
false
}
override def onClick(v: View): Unit = save()
/**
* Saves values and calls [[startMainActivity]].
*/
private def save(): Unit = {
imm.hideSoftInputFromWindow(username.getWindowToken, 0)
preferences
.edit()
.putBoolean(KeyIsFirstStart, false)
.putString(SettingsFragment.KeyUserName, username.getText.toString.trim)
.apply()
startMainActivity()
}
def startMainActivity(): Unit = {
startActivity(new Intent(this, classOf[MainActivity]))
finish()
}
}

View file

@ -3,7 +3,6 @@ package com.nutomic.ensichat.protocol
import java.util.Date
import android.app.Service
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Handler
import android.preference.PreferenceManager
@ -12,7 +11,7 @@ import com.nutomic.ensichat.R
import com.nutomic.ensichat.bluetooth.BluetoothInterface
import com.nutomic.ensichat.fragments.SettingsFragment
import com.nutomic.ensichat.protocol.ChatService.{OnConnectionsChangedListener, OnMessageReceivedListener}
import com.nutomic.ensichat.protocol.body.{UserName, MessageBody, ConnectionInfo}
import com.nutomic.ensichat.protocol.body.{ConnectionInfo, MessageBody, UserName}
import com.nutomic.ensichat.protocol.header.ContentHeader
import com.nutomic.ensichat.util.{AddContactsHandler, Database, NotificationHandler}
@ -55,6 +54,8 @@ class ChatService extends Service {
private lazy val database = new Database(this)
private lazy val preferences = PreferenceManager.getDefaultSharedPreferences(this)
private val mainHandler = new Handler()
private lazy val binder = new ChatServiceBinder(this)
@ -94,11 +95,6 @@ class ChatService extends Service {
override def onCreate(): Unit = {
super.onCreate()
val pm = PreferenceManager.getDefaultSharedPreferences(this)
if (pm.getString(SettingsFragment.KeyUserName, null) == null)
pm.edit().putString(SettingsFragment.KeyUserName,
BluetoothAdapter.getDefaultAdapter.getName).apply()
Future {
crypto.generateLocalKeys()
registerMessageListener(database)
@ -140,11 +136,10 @@ class ChatService extends Service {
if (!btInterface.getConnections.contains(target))
return
val sp = PreferenceManager.getDefaultSharedPreferences(this)
val messageId = sp.getLong("message_id", 0)
val messageId = preferences.getLong("message_id", 0)
val header = new ContentHeader(crypto.localAddress, target, seqNumGenerator.next(),
body.contentType, messageId, new Date())
sp.edit().putLong("message_id", messageId + 1)
preferences.edit().putLong("message_id", messageId + 1)
val msg = new Message(header, body)
val encrypted = crypto.encrypt(crypto.sign(msg))
@ -201,8 +196,7 @@ class ChatService extends Service {
* @return True if the connection is valid
*/
def onConnectionOpened(msg: Message): Boolean = {
val pm = PreferenceManager.getDefaultSharedPreferences(this)
val maxConnections = pm.getString(SettingsFragment.MaxConnections,
val maxConnections = preferences.getString(SettingsFragment.MaxConnections,
getResources.getString(R.string.default_max_connections)).toInt
if (connections().size == maxConnections) {
Log.i(Tag, "Maximum number of connections reached")
@ -227,7 +221,7 @@ class ChatService extends Service {
}
Log.i(Tag, "Node " + sender + " connected")
val name = PreferenceManager.getDefaultSharedPreferences(this).getString("user_name", null)
val name = preferences.getString(SettingsFragment.KeyUserName, "")
sendTo(sender, new UserName(name))
callConnectionListeners()
true