1
0
Fork 0
mirror of https://github.com/rfc2822/GfxTablet synced 2025-10-03 17:49:17 +02:00

added auto refresh and removed unused stuff

This commit is contained in:
Stephan Müller 2017-11-16 12:21:16 +01:00
parent bd49aea3d0
commit 9a74c8d1e4
15 changed files with 58 additions and 94 deletions

View file

@ -2,20 +2,14 @@ package at.bitfire.gfxtablet;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceFragment; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -23,16 +17,17 @@ import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast; import android.widget.Toast;
import at.bitfire.gfxtablet.NetEvent.Type; import at.bitfire.gfxtablet.NetEvent.Type;
public class CanvasActivity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener, SharedPreferences.OnSharedPreferenceChangeListener { public class CanvasActivity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener, SharedPreferences.OnSharedPreferenceChangeListener {
private static final int RESULT_LOAD_IMAGE = 1;
private static final String TAG = "GfxTablet.Canvas"; private static final String TAG = "GfxTablet.Canvas";
public static String SCREEN_PATH;
private static CanvasActivity instance; private static CanvasActivity instance;
private Handler autoRefreshHandler;
private Runnable autoRefreshBackground;
public static CanvasActivity get() { return instance; } public static CanvasActivity get() { return instance; }
final Uri homepageUri = Uri.parse(("https://gfxtablet.bitfire.at"));
NetworkClient netClient; NetworkClient netClient;
NetworkServer netServer; NetworkServer netServer;
@ -44,6 +39,8 @@ public class CanvasActivity extends AppCompatActivity implements View.OnSystemUi
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
instance = this; instance = this;
autoRefreshHandler = new Handler();
SCREEN_PATH = CanvasActivity.get().getCacheDir() + "/screen.png";
preferences = PreferenceManager.getDefaultSharedPreferences(this); preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
@ -64,6 +61,20 @@ public class CanvasActivity extends AppCompatActivity implements View.OnSystemUi
canvas.setNetworkClient(netClient); canvas.setNetworkClient(netClient);
} }
private void startAutoRefresh() {
autoRefreshHandler.postDelayed(new Runnable() {
public void run() {
refreshBackground();
autoRefreshBackground=this;
autoRefreshHandler.postDelayed(autoRefreshBackground, 5000);
}
}, 5000);
}
private void stopAutoRefresh() {
autoRefreshHandler.removeCallbacks(autoRefreshBackground);
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@ -73,12 +84,24 @@ public class CanvasActivity extends AppCompatActivity implements View.OnSystemUi
else else
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (preferences.getBoolean(SettingsActivity.KEY_AUTO_REFRESH, true))
startAutoRefresh();
else
stopAutoRefresh();
showTemplateImage(); showTemplateImage();
} }
@Override
protected void onPause() {
super.onPause();
stopAutoRefresh();
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
stopAutoRefresh();
netClient.getQueue().add(new NetEvent(NetEvent.Type.TYPE_DISCONNECT)); netClient.getQueue().add(new NetEvent(NetEvent.Type.TYPE_DISCONNECT));
} }
@ -118,10 +141,14 @@ public class CanvasActivity extends AppCompatActivity implements View.OnSystemUi
} }
// refresh methods // refresh methods
public void refreshBackground(MenuItem item) { public void refreshBackground() {
netClient.getQueue().add(new NetEvent(Type.TYPE_MOTION, (short) 0, (short) 0, (short) 0, -1, false)); netClient.getQueue().add(new NetEvent(Type.TYPE_MOTION, (short) 0, (short) 0, (short) 0, -1, false));
} }
public void refreshBackground(MenuItem item) {
refreshBackground();
}
// full-screen methods // full-screen methods
public void switchFullScreen(MenuItem item) { public void switchFullScreen(MenuItem item) {
@ -160,17 +187,11 @@ public class CanvasActivity extends AppCompatActivity implements View.OnSystemUi
public void showTemplateImage() { public void showTemplateImage() {
ImageView template = (ImageView)findViewById(R.id.canvas_template); ImageView template = (ImageView)findViewById(R.id.canvas_template);
template.setVisibility(View.VISIBLE); template.setVisibility(View.VISIBLE);
String picturePath = preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null); try {
if (picturePath != null) { Drawable d = Drawable.createFromPath(SCREEN_PATH);
try { template.setImageDrawable(d);
Drawable d = Drawable.createFromPath(picturePath); } catch (Exception e) {
template.setImageDrawable(d); Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
//Bitmap bm = BitmapFactory.decodeFile(picturePath);
//template.setImageBitmap(bm);
Log.i("drawn", picturePath);
} catch (Exception e) {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
} }
} }

View file

@ -31,7 +31,6 @@ public class CanvasView extends View implements SharedPreferences.OnSharedPrefer
// setup // setup
public CanvasView(Context context, AttributeSet attributeSet) { public CanvasView(Context context, AttributeSet attributeSet) {
super(context, attributeSet); super(context, attributeSet);
@ -40,7 +39,6 @@ public class CanvasView extends View implements SharedPreferences.OnSharedPrefer
settings = PreferenceManager.getDefaultSharedPreferences(context); settings = PreferenceManager.getDefaultSharedPreferences(context);
settings.registerOnSharedPreferenceChangeListener(this); settings.registerOnSharedPreferenceChangeListener(this);
setBackground();
setInputMethods(); setInputMethods();
inRangeStatus = InRangeStatus.OutOfRange; inRangeStatus = InRangeStatus.OutOfRange;
} }
@ -52,27 +50,14 @@ public class CanvasView extends View implements SharedPreferences.OnSharedPrefer
// settings // settings
protected void setBackground() {
if (settings.getBoolean(SettingsActivity.KEY_DARK_CANVAS, false))
setBackgroundColor(Color.BLACK);
//else
//setBackgroundResource(R.drawable.bg_grid_pattern); //<-- Add this as option?
}
protected void setInputMethods() { protected void setInputMethods() {
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false); acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) { if (key.equals(SettingsActivity.KEY_PREF_STYLUS_ONLY)) {
case SettingsActivity.KEY_PREF_STYLUS_ONLY: setInputMethods();
setInputMethods();
break;
case SettingsActivity.KEY_DARK_CANVAS:
setBackground();
break;
} }
} }

View file

@ -23,34 +23,34 @@ public class NetworkServer implements Runnable {
public void run() { public void run() {
try { try {
DatagramSocket socket = new DatagramSocket(GFXTABLET_PORT); DatagramSocket socket = new DatagramSocket(GFXTABLET_PORT);
int packets = 0;
SparseArray<byte[]> buffer = new SparseArray<>(); SparseArray<byte[]> buffer = new SparseArray<>();
// Init has to be done twice because the first call will be set on the server with 0.0.0.0 // Init has to be done twice because the first call will be set on the server with 0.0.0.0
// but we need the ip of the client. // but we need the ip of the client.
CanvasActivity.get().sendMotionStopSignal(); CanvasActivity.get().sendMotionStopSignal();
CanvasActivity.get().sendMotionStopSignal(); CanvasActivity.get().sendMotionStopSignal();
CanvasActivity.get().refreshBackground();
while (true) { while (true) {
byte[] buf = new byte[60030]; byte[] buf = new byte[60030];
DatagramPacket packet = new DatagramPacket(buf, buf.length); DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet); socket.receive(packet);
int n = buf[60029]; int n = buf[60029];
//Log.i("receive:", String.valueOf(n)); Log.i("receive:", String.valueOf(n));
if (n != 0){ if (n != 0){
buffer.put(n, buf); buffer.put(n, buf);
} else if (buffer.size() > 0 ) { } else if (buffer.size() > 0 ) {
try { try {
String path = CanvasActivity.get().getCacheDir() + "/dsektop.png"; String path = CanvasActivity.SCREEN_PATH;
Log.i("buffer:", String.valueOf(buf[0]));
boolean parts = buffer.size() == (int) buf[0]; boolean parts = buffer.size() == (int) buf[0];
for (int i=0; i < buf[0]; i++) { for (int i=0; i < buf[0]; i++) {
if (parts) { if (parts) {
//Log.i("keyAt " + i, String.valueOf(buffer.keyAt(i)));
parts = buffer.keyAt(i) == i+1; parts = buffer.keyAt(i) == i+1;
} }
} }
if (!parts) { if (!parts) {
buffer.clear(); buffer.clear();
CanvasActivity.get().sendMotionStopSignal(); CanvasActivity.get().refreshBackground();
Log.i("Image Problem", "tying to refetch the screenshot"); Log.i("Image Problem", "tying to refetch the screenshot");
continue; continue;
} }
@ -62,13 +62,6 @@ public class NetworkServer implements Runnable {
} }
fos.flush(); fos.flush();
fos.close(); fos.close();
File file = new File(path);
long size = file.length();
//Log.i("file-path", path);
//Log.i("file-size", String.valueOf(size));
preferences.edit().remove(SettingsActivity.KEY_TEMPLATE_IMAGE).apply();
preferences.edit().putString(SettingsActivity.KEY_TEMPLATE_IMAGE, path).apply();
//Log.i("file-path-current", preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null));
CanvasActivity.get().runOnUiThread(new Runnable() { CanvasActivity.get().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {

View file

@ -8,9 +8,8 @@ public class SettingsActivity extends AppCompatActivity {
public static final String public static final String
KEY_PREF_HOST = "host_preference", KEY_PREF_HOST = "host_preference",
KEY_PREF_STYLUS_ONLY = "stylus_only_preference", KEY_PREF_STYLUS_ONLY = "stylus_only_preference",
KEY_DARK_CANVAS = "dark_canvas_preference",
KEY_KEEP_DISPLAY_ACTIVE = "keep_display_active_preference", KEY_KEEP_DISPLAY_ACTIVE = "keep_display_active_preference",
KEY_TEMPLATE_IMAGE = "key_template_image"; KEY_AUTO_REFRESH = "auto_refresh_preference";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 771 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 558 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 947 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 251 B

View file

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap
android:src="@drawable/bg_grid"
android:tileMode="repeat"
xmlns:android="http://schemas.android.com/apk/res/android"/>

View file

@ -6,7 +6,7 @@
<ImageView <ImageView
android:id="@+id/canvas_template" android:id="@+id/canvas_template"
android:src="@drawable/ic_action_picture" android:src="@drawable/ic_renew"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:scaleType="fitXY"/> android:scaleType="fitXY"/>

View file

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_clear_template_image"
android:onClick="clearTemplateImage"
android:title="@string/menu_clear_template_image"/>
<item
android:id="@+id/menu_select_template_image"
android:onClick="selectTemplateImage"
android:title="@string/menu_select_another_template_image"/>
</menu>

View file

@ -1,15 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">GfxTablet</string> <string name="app_name">GfxTablet</string>
<string name="menu_settings">Settings</string> <string name="menu_settings">Settings</string>
<string name="menu_about">About / Help</string>
<string name="menu_donate">Donate</string>
<string name="fullscreen">Full-screen mode</string> <string name="fullscreen">Full-screen mode</string>
<string name="menu_set_template_image">Set template image</string>
<string name="menu_clear_template_image">Clear template image</string>
<string name="menu_select_another_template_image">Select another image</string>
<string name="no_host_defined">No valid recipient host defined. Please configure in \"Settings / Recipient host\".</string> <string name="no_host_defined">No valid recipient host defined. Please configure in \"Settings / Recipient host\".</string>
@ -18,11 +12,9 @@
<string name="preferences_stylus_only">Sense stylus only</string> <string name="preferences_stylus_only">Sense stylus only</string>
<string name="preferences_stylus_only_on">Only stylus input will be processed</string> <string name="preferences_stylus_only_on">Only stylus input will be processed</string>
<string name="preferences_stylus_only_off">Finger and stylus input will be processed</string> <string name="preferences_stylus_only_off">Finger and stylus input will be processed</string>
<string name="preferences_dark_canvas">Use dark canvas</string>
<string name="preferences_dark_canvas_on">Black canvas will be used (covers template image)</string>
<string name="preferences_dark_canvas_off">White/transparent gridded canvas will be used</string>
<string name="preferences_keep_display_active">Keep display active</string> <string name="preferences_keep_display_active">Keep display active</string>
<string name="preferences_keep_display_active_on">Display won\'t turn off while GfxTablet is active</string> <string name="preferences_keep_display_active_on">Display won\'t turn off while GfxTablet is active</string>
<string name="preferences_keep_display_active_off">Display will turn off according to system settings</string> <string name="preferences_keep_display_active_off">Display will turn off according to system settings</string>
<string name="auto_reload_summary">It will refresh every 5 seconds.</string>
<string name="auto_reload_title">Auto refresh</string>
</resources> </resources>

View file

@ -1,9 +1,6 @@
<resources> <resources>
<style name="BaseTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar"> <style name="BaseTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
</style> </style>
<style name="AppTheme" parent="BaseTheme"> <style name="AppTheme" parent="BaseTheme">
</style> </style>
</resources> </resources>

View file

@ -16,20 +16,16 @@
android:title="@string/preferences_keep_display_active" android:title="@string/preferences_keep_display_active"
android:summaryOn="@string/preferences_keep_display_active_on" android:summaryOn="@string/preferences_keep_display_active_on"
android:summaryOff="@string/preferences_keep_display_active_off" /> android:summaryOff="@string/preferences_keep_display_active_off" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="stylus_only_preference" android:key="stylus_only_preference"
android:title="@string/preferences_stylus_only" android:title="@string/preferences_stylus_only"
android:summaryOn="@string/preferences_stylus_only_on" android:summaryOn="@string/preferences_stylus_only_on"
android:summaryOff="@string/preferences_stylus_only_off" /> android:summaryOff="@string/preferences_stylus_only_off" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="dark_canvas_preference" android:key="auto_refresh_preference"
android:title="@string/preferences_dark_canvas" android:summary="@string/auto_reload_summary"
android:summaryOn="@string/preferences_dark_canvas_on" android:title="@string/auto_reload_title" />
android:summaryOff="@string/preferences_dark_canvas_off" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>