Skip to content

Commit

Permalink
Add settings button and activity
Browse files Browse the repository at this point in the history
* for lighting / start up behavior
* for notifications sounds
* use default shared preferences (which are by default in PRIVATE_MODE)
* use the resource strings as identifiers rather than their integer values that to not stay constant between builds

As the settings activity does not run in fullscreen, this helps with kloener#8
  • Loading branch information
morckx committed Sep 26, 2020
1 parent 76c2244 commit b8adc1f
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 48 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dependencies {
implementation 'com.github.chrisbanes:PhotoView:2.3.0'
implementation 'androidx.core:core-ktx:1.5.0-alpha03'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.preference:preference:1.1.1'
}

repositories {
Expand Down
44 changes: 29 additions & 15 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.visorapp.visor">
<!-- access the camera -->
<uses-permission android:name="android.permission.CAMERA" />
<!--
Store pictures on the sd card
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /
-->
<!-- the current device has to have a camera with autofocus and a flashlight -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" />

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />

<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
android:theme="@style/AppTheme">
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"></activity>
<activity
android:name=".VisorActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"
android:screenOrientation="landscape">
android:screenOrientation="landscape"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
android:resource="@xml/provider_paths" />
</provider>
</application>
<!-- access the camera -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Store pictures on the sd card
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /-->
<!-- the current device has to have a camera with autofocus and a flashlight -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>

</manifest>
</manifest>
35 changes: 35 additions & 0 deletions app/src/main/java/de/visorapp/visor/SettingsActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.visorapp.visor

import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat

class SettingsActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.getItemId()) {
android.R.id.home -> {
super.onBackPressed()
return true
}
}
return super.onOptionsItemSelected(item)
}

class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
}
25 changes: 23 additions & 2 deletions app/src/main/java/de/visorapp/visor/VisorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
Expand All @@ -24,6 +25,7 @@
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;

import com.github.chrisbanes.photoview.PhotoView;

Expand Down Expand Up @@ -81,11 +83,12 @@ public class VisorActivity extends Activity {
* stores the brightness level of the screen to restore it after the
* app gets paused or destroyed.
*/
private float prevScreenBrightnewss;
private float prevScreenBrightnewss = -1f;
public PhotoView mPhotoView;

private int zoomSliderVisibility = View.VISIBLE;
private View mVisorViewTouchArea;
private SharedPreferences mSharedPreferences;

public void playClickSound(View view) {
// TODO the user can disable this; if click-sounds are enable I hear a double click effect...
Expand Down Expand Up @@ -179,6 +182,14 @@ private void cameraPreviewIsActive(ImageButton playOrPauseButton) {
}
}

private View.OnClickListener openSettingsClickHandler = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
}
};

private View.OnClickListener flashLightClickHandler = new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -260,9 +271,12 @@ protected void setBrightnessToMaximum() {
* resets the brightness value to the previous screen value.
*/
protected void resetBrightnessToPreviousValue() {
if (prevScreenBrightnewss < 0)
return;
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = prevScreenBrightnewss;
getWindow().setAttributes(layout);
prevScreenBrightnewss = -1f;
}

/**
Expand Down Expand Up @@ -352,6 +366,7 @@ private void restartActvitiy() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
setContentView(R.layout.activity_visor);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
Expand Down Expand Up @@ -400,6 +415,9 @@ private FrameLayout getCameraPreviewFrame() {
*
*/
private void setButtonListeners() {
ImageButton settingsButtonm = findViewById(R.id.settings_button);
settingsButtonm.setOnClickListener(openSettingsClickHandler);

// Add a listener to the Zoom slider
SeekBar zoomSlider = findViewById(R.id.zoom_slider);
zoomSlider.setOnSeekBarChangeListener(zoomSliderChangelistener);
Expand Down Expand Up @@ -432,7 +450,7 @@ protected void onPause() {
super.onPause();
// 2015-10-19 ChangeRequest: Some users have problems with the high brightness value.
// So the user now has to activly adjust the brightness.
// resetBrightnessToPreviousValue();
resetBrightnessToPreviousValue();
Log.d(TAG, "onPause called!");
}

Expand All @@ -445,6 +463,9 @@ protected void onDestroy() {
@Override
protected void onResume() {
super.onResume();
if (mSharedPreferences.getBoolean(getResources().getString(R.string.key_preference_max_brightness), false)) {
setBrightnessToMaximum();
}

if (cameraPreviewState != true && mPhotoView != null) {
cameraPreviewState = true;
Expand Down
51 changes: 31 additions & 20 deletions app/src/main/java/de/visorapp/visor/VisorSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.preference.PreferenceManager;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -79,7 +81,9 @@ public class VisorSurface extends SurfaceView implements SurfaceHolder.Callback,
* Max width for the camera preview to avoid performance and ram/cache issues.
* TODO should be configurable by a settings-activity! (feature)
*/

private static final int MAX_CAMERA_PREVIEW_RESOLUTION_WIDTH = 1024;
private final SharedPreferences mSharedPreferences;

private MediaActionSound mSound = null;

Expand Down Expand Up @@ -334,10 +338,10 @@ public VisorSurface(Context context) {
mCameraMaxZoomLevel = 0;
mCurrentColorFilterIndex = 0;

SharedPreferences sharedPreferences = context.getSharedPreferences(String.valueOf(R.string.visor_shared_preference_name), Context.MODE_PRIVATE);
mCameraCurrentZoomLevel = sharedPreferences.getInt(String.valueOf(R.string.key_preference_zoom_level), mCameraCurrentZoomLevel);
mCurrentColorFilterIndex = sharedPreferences.getInt(String.valueOf(R.string.key_preference_color_mode), mCurrentColorFilterIndex);
storedAutoFocusMode = sharedPreferences.getString(String.valueOf(R.string.key_preference_autofocus_mode), FOCUS_MODE_AUTO);
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
mCameraCurrentZoomLevel = mSharedPreferences.getInt(getResources().getString(R.string.key_preference_zoom_level), mCameraCurrentZoomLevel);
mCurrentColorFilterIndex = mSharedPreferences.getInt(getResources().getString(R.string.key_preference_color_mode), mCurrentColorFilterIndex);
storedAutoFocusMode = mSharedPreferences.getString(getResources().getString(R.string.key_preference_autofocus_mode), FOCUS_MODE_AUTO);

mCameraFlashMode = false;
mColorFilterPaint = new Paint();
Expand All @@ -349,7 +353,7 @@ public VisorSurface(Context context) {
Point sizePoint = new Point();

mDisplay.getSize(sizePoint);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// getting a preciser value of the screen size to be more accurate.
mDisplay.getRealSize(sizePoint);
}
Expand Down Expand Up @@ -429,12 +433,11 @@ public void surfaceDestroyed(SurfaceHolder holder) {
}
}

SharedPreferences sharedPreferences = this.getContext().getSharedPreferences(String.valueOf(R.string.visor_shared_preference_name), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
SharedPreferences.Editor editor = mSharedPreferences.edit();

editor.putInt(String.valueOf(R.string.key_preference_zoom_level), mCameraCurrentZoomLevel);
editor.putInt(String.valueOf(R.string.key_preference_color_mode), mCurrentColorFilterIndex);
editor.putString(String.valueOf(R.string.key_preference_autofocus_mode), currentFocusMode);
editor.putInt(getResources().getString(R.string.key_preference_zoom_level), mCameraCurrentZoomLevel);
editor.putInt(getResources().getString(R.string.key_preference_color_mode), mCurrentColorFilterIndex);
editor.putString(getResources().getString(R.string.key_preference_autofocus_mode), currentFocusMode);

editor.apply();

Expand Down Expand Up @@ -584,7 +587,7 @@ public void enableCamera() {
} else {
setCameraZoomLevel(mCameraCurrentZoomLevel);
}

setSZoomSliderLevelPercent(mCameraCurrentZoomLevel * 100 / mCameraMaxZoomLevel);
if (mCurrentColorFilterIndex > 0) {
mCurrentColorFilterIndex--;
// decrease index because
Expand All @@ -597,6 +600,9 @@ public void enableCamera() {
}

autoFocusCamera();
if (mSharedPreferences.getBoolean(getResources().getString(R.string.key_preference_auto_torch), false)) {
turnFlashlightOn();
}

Log.d(TAG, "Thread done. Camera successfully started");
}
Expand Down Expand Up @@ -671,18 +677,23 @@ public MediaActionSound getMediaActionSound() {
}

public void playActionSoundAutofocusComplete() {
MediaActionSound player = getMediaActionSound();
if (player == null) return;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return;
player.play(MediaActionSound.FOCUS_COMPLETE);
return;
if (mSharedPreferences.getBoolean(getResources()
.getString(R.string.key_preference_autofocus_sound), false)) {
MediaActionSound player = getMediaActionSound();
if (player == null) return;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return;
player.play(MediaActionSound.FOCUS_COMPLETE);
}
}

public void playActionSoundShutter() {
MediaActionSound player = getMediaActionSound();
if (player == null) return;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return;
player.play(MediaActionSound.SHUTTER_CLICK);
if (mSharedPreferences.getBoolean(getResources()
.getString(R.string.key_preference_shutter_sound), false)) {
MediaActionSound player = getMediaActionSound();
if (player == null) return;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return;
player.play(MediaActionSound.SHUTTER_CLICK);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_baseline_flare_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M7,11L1,11v2h6v-2zM9.17,7.76L7.05,5.64 5.64,7.05l2.12,2.12 1.41,-1.41zM13,1h-2v6h2L13,1zM18.36,7.05l-1.41,-1.41 -2.12,2.12 1.41,1.41 2.12,-2.12zM17,11v2h6v-2h-6zM12,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3 3,-1.34 3,-3 -1.34,-3 -3,-3zM14.83,16.24l2.12,2.12 1.41,-1.41 -2.12,-2.12 -1.41,1.41zM5.64,16.95l1.41,1.41 2.12,-2.12 -1.41,-1.41 -2.12,2.12zM11,23h2v-6h-2v6z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_baseline_notifications_none_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zM18,16v-5c0,-3.07 -1.63,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.64,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2zM16,17L8,17v-6c0,-2.48 1.51,-4.5 4,-4.5s4,2.02 4,4.5v6z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_settings_grey_48.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="48dp" android:tint="#808080"
android:viewportHeight="24" android:viewportWidth="24"
android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_visor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,17 @@
android:layout_alignParentRight="true"
android:elevation="8dp" />

<ImageButton
android:id="@+id/settings_button"
android:layout_width="@dimen/button_size_third"
android:layout_height="@dimen/button_size_third"
android:scaleType="centerInside"
android:layout_margin="@dimen/button_margin_secondary"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="?android:attr/selectableItemBackground"
android:src="@drawable/ic_baseline_settings_grey_48"
/>


</RelativeLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/settings_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Loading

0 comments on commit b8adc1f

Please sign in to comment.