Skip to content

Commit

Permalink
feat: Remember server selection (#326)
Browse files Browse the repository at this point in the history
* feat: Remember server selection

* Update ALVRActivity.java
  • Loading branch information
ShootingKing-AM authored Apr 14, 2024
1 parent 29850b6 commit 54cefdc
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
7 changes: 4 additions & 3 deletions code/mobile/android/PhoneVR/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
android:resource="@xml/provider_paths_app" />
</provider>
<activity
android:name=".MainActivity"/>
android:name=".MainActivity"
android:launchMode="singleTop" />
<activity
android:name=".GameActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
Expand All @@ -87,8 +88,8 @@
<activity
android:name=".ALVRActivity"
android:screenOrientation="landscape"
android:theme="@style/Theme.AppCompat.NoActionBar">
</activity>
android:theme="@style/Theme.AppCompat.NoActionBar"
android:launchMode="singleTop" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.switch_viewer) {
switchViewerNative();
return true;
} else if (item.getItemId() == R.id.switch_server) {
// remove alvr_server prefs
SharedPreferences.Editor editor = getSharedPreferences("prefs", MODE_PRIVATE).edit();
editor.remove("alvr_server");
editor.apply();

// start InitActivity and clear back history
Intent intent = new Intent(this, InitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else if (item.getItemId() == R.id.max_brightness_toggle) {
// Save app setting boolean max_brightness == true
item.setChecked(!item.isChecked());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ package viritualisres.phonevr
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.Html.FROM_HTML_MODE_LEGACY
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
import androidx.core.text.HtmlCompat

class InitActivity : AppCompatActivity() {
Expand All @@ -34,9 +34,37 @@ class InitActivity : AppCompatActivity() {
tvBody.text = String.format(resources.getString(R.string.phonevr_support_packages), result)
tvBody.movementMethod = LinkMovementMethod.getInstance()
Linkify.addLinks(tvBody, Linkify.WEB_URLS)

val prefs = getSharedPreferences("prefs", MODE_PRIVATE)
val swVRemember = findViewById<SwitchCompat>(R.id.remeber_server)
swVRemember.isChecked = prefs.getBoolean("remember", false)

// check if alvr_server setting exists == first run
// if user has chosen to show server selection menu from respective main activities,
// ALVRActivity or MainActivity
// Not kept in Resume() to prevent looping when coming back from AlvrActivity, or others
if (prefs.contains("alvr_server") && prefs.getBoolean("remember", false)) {
var intent = Intent(this, ALVRActivity::class.java)
if (!prefs.getBoolean("alvr_server", true)) {
intent = Intent(this, MainActivity::class.java)
}
startActivity(intent)
}

swVRemember.setOnCheckedChangeListener { _, isChecked ->
prefs.edit().putBoolean("remember", isChecked).apply()
if (!isChecked) {
prefs.edit().remove("alvr_server").apply()
}
}
}

fun btOnClickALVRStreamer(view: View) {
val prefs = getSharedPreferences("prefs", MODE_PRIVATE)
// Save choice after first open. alvr_server as boolean
if (prefs.getBoolean("remember", false))
prefs.edit().putBoolean("alvr_server", true).apply()

val intent = Intent(this, ALVRActivity::class.java)
startActivity(intent)
}
Expand All @@ -47,6 +75,11 @@ class InitActivity : AppCompatActivity() {
if (Build.SUPPORTED_ABIS.contains("x86") ||
Build.SUPPORTED_ABIS.contains("arm64-v8a") ||
Build.SUPPORTED_ABIS.contains("armeabi-v7a")) {
val prefs = getSharedPreferences("prefs", MODE_PRIVATE)

if (prefs.getBoolean("remember", false))
prefs.edit().putBoolean("alvr_server", false).apply()

val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ class SettingsActivity : AppCompatActivity() {
OpenFileWithIntent(File(getExternalFilesDir(null).toString() + "/PVR/pvrlog.txt"))
}
}

val btSwitchServer: Button = findViewById(R.id.btSwitchServer)
btSwitchServer.setOnClickListener {
val prefs = getSharedPreferences("prefs", MODE_PRIVATE)
prefs.edit().remove("alvr_server").apply()

// Start initActivity and let user choose the server again. Clear back history
val intent = Intent(this@SettingsActivity, InitActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
}
}
/*
* Zips a file at a location and places the resulting zip file at the toLocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@
app:layout_constraintTop_toTopOf="@+id/alvr_streamer"
app:srcCompat="@mipmap/ic_launcher_alvr" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/remeber_server"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/remember_choice"
android:textAlignment="viewStart"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pvr_streamer"
app:switchPadding="10dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@
android:text="@string/share_logs"/>
</LinearLayout>

<Button
android:id="@+id/btSwitchServer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="@string/switch_phonevr_server"
app:layout_constraintBottom_toTopOf="@id/textViewLog"
app:layout_constraintTop_toBottomOf="@+id/btLayout"
tools:layout_editor_absoluteX="10dp" />

<TextView
android:id="@+id/textViewLog"
android:layout_width="0dp"
Expand All @@ -297,7 +312,7 @@
android:text="@string/log_recent_100_lines"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btLayout"
app:layout_constraintTop_toBottomOf="@+id/btSwitchServer"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
android:layout_height="match_parent">
<item android:id="@+id/switch_viewer"
android:title="@string/switch_viewer" />
<item android:id="@+id/switch_server"
android:title="@string/switch_phonevr_server" />
<item
android:id="@+id/max_brightness_toggle"
android:title="@string/max_brightness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@
<string name="switch_viewer">
Switch Cardboard viewer
</string>
<string name="switch_phonevr_server">Switch PhoneVR Server</string>
<string name="remember_choice">Remember choice</string>
<string name="max_brightness">Max Brightness</string>
</resources>

0 comments on commit 54cefdc

Please sign in to comment.