Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
axel7083 committed Mar 23, 2021
1 parent a75b449 commit 85642de
Show file tree
Hide file tree
Showing 39 changed files with 705 additions and 250 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</activity>

<service
android:name=".services.FloatingView"
android:name=".services.FloatingViewService"
android:enabled="true"
android:exported="false" />

Expand Down
47 changes: 47 additions & 0 deletions app/src/main/java/com/github/facecommands/Utils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
package com.github.facecommands;

import android.app.ActivityManager;
import android.content.Context;
import android.provider.Settings;
import android.util.Log;

import com.github.facecommands.camera.GraphicOverlay;
import com.github.facecommands.faces.GraphicFaceTrackerFactory;
import com.github.facecommands.faces.OnFaceGesture;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.MultiProcessor;
import com.google.android.gms.vision.face.FaceDetector;

public class Utils {

private final static String TAG = "Utils";

public static boolean isAccessServiceEnabled(Context context, Class accessibilityServiceClass)
{
String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
return prefString!= null && prefString.contains(context.getPackageName() + "/" + accessibilityServiceClass.getName());
}


public static boolean isServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

/**
* Creates and starts the camera. Note that this uses a higher resolution in comparison
* to other detection examples to enable the barcode detector to detect small barcodes
* at long distances.
*/
public static CameraSource createCameraSource(Context context, GraphicOverlay graphicOverlay, OnFaceGesture onFaceGesture) {

FaceDetector detector = new FaceDetector.Builder(context)
.setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
.build();

detector.setProcessor(
new MultiProcessor.Builder<>(new GraphicFaceTrackerFactory(graphicOverlay, onFaceGesture))
.build());

if (!detector.isOperational()) {
Log.w(TAG, "Face detector dependencies are not yet available.");
}

return new CameraSource.Builder(context, detector)
.setRequestedPreviewSize(640, 480)
.setFacing(CameraSource.CAMERA_FACING_FRONT)
.setRequestedFps(25.0f)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.github.facecommands.services.FloatingView
import com.github.facecommands.Utils
import com.github.facecommands.services.FloatingViewService
import com.github.facecommands.Utils.isAccessServiceEnabled
import com.github.facecommands.databinding.ActivityMainBinding
import com.github.facecommands.services.AutoService


class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.OnCheckedChangeListener, AdapterView.OnItemSelectedListener {
Expand All @@ -25,7 +27,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O
private const val RC_HANDLE_CAMERA_PERM = 2
private const val RC_HANDLE_ACCESSIBILITY = 3
private const val RC_HANDLE_SETTINGS = 4
private const val MY_PREFS = "faceCommands"
public const val MY_PREFS = "faceCommands"
private const val TAG = "MainActivity"
}

Expand All @@ -41,11 +43,10 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)


loadSettings()

// Setup switch
refreshSwitches()
refreshStartBtn()


// Setup spinner
Expand All @@ -68,9 +69,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O
binding.rightEyeSpinner.onItemSelectedListener = this
binding.leftEyeSpinner.onItemSelectedListener = this



//findViewById<View>(R.id.startFloat).setOnClickListener(this)
binding.startFloat.setOnClickListener(this)
}

private fun saveSettings() {
Expand Down Expand Up @@ -122,16 +121,21 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O


override fun onClick(v: View?) {
if (Settings.canDrawOverlays(this)) {
startService(Intent(this@MainActivity, FloatingView::class.java))
finish()
} else {
askPermission()
Toast.makeText(
this,
"You need System Alert Window Permission to do this",
Toast.LENGTH_SHORT
).show()

if(v == null)
return

if(v.id == binding.startFloat.id ) {
if(Utils.isServiceRunning(FloatingViewService::class.java, this)) {
binding.startFloat.text = "Already running"
binding.startFloat.isEnabled = false
return
}
else
{
startService(Intent(this@MainActivity, FloatingViewService::class.java))
finish()
}
}
}

Expand Down Expand Up @@ -167,11 +171,11 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O
"Camera permission granted - initialize the camera source"
)
// we have permission
refreshSwitches()
refreshStartBtn()
return
}
Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show()
refreshSwitches()
refreshStartBtn()
}

override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
Expand All @@ -192,13 +196,10 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O
openSettings()
}
else if(p0.id == binding.accessibilitySwitch.id) {
if(p1)
startActivityForResult(
Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS),
RC_HANDLE_ACCESSIBILITY
);
else
openSettings()
startActivityForResult(
Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS),
RC_HANDLE_ACCESSIBILITY
)
}
}

Expand All @@ -214,18 +215,18 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_HANDLE_ACCESSIBILITY) {
if (resultCode == RESULT_OK) {
refreshSwitches()
refreshStartBtn()
}
if (resultCode == RESULT_CANCELED) {
Log.d(TAG,"RC_HANDLE_ACCESSIBILITY cancel")
refreshSwitches()
refreshStartBtn()

if(!binding.accessibilitySwitch.isChecked)
Toast.makeText(this,"You need to activate the \"Blink to scroll\" service to use this application.",Toast.LENGTH_LONG).show()
}
}
else if(requestCode == RC_HANDLE_SETTINGS) {
refreshSwitches()
refreshStartBtn()
}
} //onActivityResult

Expand All @@ -243,11 +244,30 @@ class MainActivity : AppCompatActivity(), View.OnClickListener, CompoundButton.O
) == PackageManager.PERMISSION_GRANTED
binding.accessibilitySwitch.isChecked = isAccessServiceEnabled(
this,
FloatingView::class.java
AutoService::class.java
)

binding.appearOnTopSwitch.setOnCheckedChangeListener(this)
binding.cameraSwitch.setOnCheckedChangeListener(this)
binding.accessibilitySwitch.setOnCheckedChangeListener(this)
}

private fun checkPermissions() : Boolean {
refreshSwitches()
return binding.appearOnTopSwitch.isChecked && binding.cameraSwitch.isChecked && binding.accessibilitySwitch.isChecked
}

private fun refreshStartBtn() {

if(checkPermissions()) {
binding.startFloat.text = "Start"
binding.startFloat.isEnabled = true
}
else
{
binding.startFloat.text = "Start"
binding.startFloat.isEnabled = false
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package com.github.facecommands.camera;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import androidx.core.app.ActivityCompat;

import com.google.android.gms.common.images.Size;
import com.google.android.gms.vision.CameraSource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

Expand Down Expand Up @@ -51,7 +52,7 @@ public class GraphicOverlay extends View {
private float mHeightScaleFactor = 1.0f;
private int mFacing = CameraSource.CAMERA_FACING_BACK;
private Set<Graphic> mGraphics = new HashSet<>();

private static final int BACKGROUND = Color.TRANSPARENT;
/**
* Base class for a custom graphics object to be rendered within the graphic overlay. Subclass
* this and implement the {@link Graphic#draw(Canvas)} method to define the
Expand Down Expand Up @@ -128,6 +129,7 @@ public GraphicOverlay(Context context, AttributeSet attrs) {
public void clear() {
synchronized (mLock) {
mGraphics.clear();

}
postInvalidate();
}
Expand Down Expand Up @@ -173,6 +175,8 @@ protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

synchronized (mLock) {
canvas.drawColor(BACKGROUND);

if ((mPreviewWidth != 0) && (mPreviewHeight != 0)) {
mWidthScaleFactor = (float) canvas.getWidth() / (float) mPreviewWidth;
mHeightScaleFactor = (float) canvas.getHeight() / (float) mPreviewHeight;
Expand All @@ -181,6 +185,9 @@ protected void onDraw(Canvas canvas) {
for (Graphic graphic : mGraphics) {
graphic.draw(canvas);
}


//setBackgroundColor(Color.TRANSPARENT);
}
}
}
Loading

0 comments on commit 85642de

Please sign in to comment.