Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit f64df04

Browse files
committed
Change template image logic
1 parent fb20c3f commit f64df04

18 files changed

+188
-89
lines changed

app-android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ android {
1919
}
2020

2121
dependencies {
22-
compile "com.android.support:appcompat-v7:21.0.+"
22+
compile "com.android.support:appcompat-v7:22.+"
2323
}

app-android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
android:versionCode="4"
55
android:versionName="1.3" >
66

7-
<uses-sdk
8-
android:minSdkVersion="14"
9-
android:targetSdkVersion="22" />
7+
<uses-sdk />
108

119
<uses-permission android:name="android.permission.INTERNET" />
1210
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
@@ -18,7 +16,8 @@
1816
android:allowBackup="true"
1917
android:icon="@drawable/ic_launcher"
2018
android:theme="@style/AppTheme"
21-
android:label="@string/app_name" >
19+
android:label="@string/app_name"
20+
android:largeHeap="true">
2221
<activity
2322
android:name="at.bitfire.gfxtablet.CanvasActivity"
2423
android:label="@string/app_name"

app-android/app/src/main/java/at/bitfire/gfxtablet/CanvasActivity.java

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,26 @@
99
import android.os.AsyncTask;
1010
import android.os.Build;
1111
import android.os.Bundle;
12+
import android.preference.PreferenceFragment;
1213
import android.preference.PreferenceManager;
1314
import android.provider.MediaStore;
14-
import android.support.v7.app.ActionBarActivity;
15+
import android.support.v7.app.AppCompatActivity;
1516
import android.support.v7.widget.PopupMenu;
17+
import android.util.Log;
1618
import android.view.Menu;
1719
import android.view.MenuItem;
1820
import android.view.View;
1921
import android.view.WindowManager;
22+
import android.widget.ImageView;
2023
import android.widget.Toast;
2124

22-
public class CanvasActivity extends ActionBarActivity implements View.OnSystemUiVisibilityChangeListener {
23-
private static int RESULT_LOAD_IMAGE = 1;
25+
public class CanvasActivity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener, SharedPreferences.OnSharedPreferenceChangeListener {
26+
private static final int RESULT_LOAD_IMAGE = 1;
27+
private static final String TAG = "GfxTablet.Canvas";
28+
29+
final Uri homepageUri = Uri.parse(("https://rfc2822.github.io/GfxTablet/"));
2430

2531
NetworkClient netClient;
26-
CanvasView canvas;
2732

2833
SharedPreferences preferences;
2934
boolean fullScreen = false;
@@ -34,18 +39,18 @@ protected void onCreate(Bundle savedInstanceState) {
3439
super.onCreate(savedInstanceState);
3540

3641
preferences = PreferenceManager.getDefaultSharedPreferences(this);
42+
preferences.registerOnSharedPreferenceChangeListener(this);
43+
44+
setContentView(R.layout.activity_canvas);
3745

46+
// create network client in a separate thread
3847
netClient = new NetworkClient(PreferenceManager.getDefaultSharedPreferences(this));
3948
new Thread(netClient).start();
40-
41-
canvas = new CanvasView(CanvasActivity.this, netClient);
42-
}
43-
44-
@Override
45-
protected void onStart() {
46-
super.onStart();
47-
4849
new ConfigureNetworkingTask().execute();
50+
51+
// notify CanvasView of the network client
52+
CanvasView canvas = (CanvasView)findViewById(R.id.canvas);
53+
canvas.setNetworkClient(netClient);
4954
}
5055

5156
@Override
@@ -61,9 +66,9 @@ protected void onResume() {
6166
}
6267

6368
@Override
64-
protected void onStop() {
69+
protected void onDestroy() {
70+
super.onDestroy();
6571
netClient.getQueue().add(new NetEvent(NetEvent.Type.TYPE_DISCONNECT));
66-
super.onStop();
6772
}
6873

6974
@Override
@@ -81,14 +86,33 @@ public void onBackPressed() {
8186
}
8287

8388
public void showAbout(MenuItem item) {
84-
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(("https://rfc2822.github.io/GfxTablet/"))));
89+
startActivity(new Intent(Intent.ACTION_VIEW, homepageUri));
90+
}
91+
92+
public void showDonate(MenuItem item) {
93+
startActivity(new Intent(Intent.ACTION_VIEW, homepageUri.buildUpon().fragment("donate").build()));
8594
}
8695

8796
public void showSettings(MenuItem item) {
8897
startActivityForResult(new Intent(this, SettingsActivity.class), 0);
8998
}
9099

91100

101+
// preferences were changed
102+
103+
@Override
104+
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
105+
switch (key) {
106+
case SettingsActivity.KEY_PREF_HOST:
107+
Log.i(TAG, "Recipient host changed, reconfiguring network client");
108+
new ConfigureNetworkingTask().execute();
109+
break;
110+
}
111+
}
112+
113+
114+
// full-screen methods
115+
92116
public void switchFullScreen(MenuItem item) {
93117
final View decorView = getWindow().getDecorView();
94118
int uiFlags = decorView.getSystemUiVisibility();
@@ -97,7 +121,7 @@ public void switchFullScreen(MenuItem item) {
97121
uiFlags ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
98122
if (Build.VERSION.SDK_INT >= 16)
99123
uiFlags ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
100-
if (Build.VERSION.SDK_INT >= 18)
124+
if (Build.VERSION.SDK_INT >= 19)
101125
uiFlags ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
102126

103127
decorView.setOnSystemUiVisibilityChangeListener(this);
@@ -106,16 +130,21 @@ public void switchFullScreen(MenuItem item) {
106130

107131
@Override
108132
public void onSystemUiVisibilityChange(int visibility) {
133+
Log.i("GfxTablet", "System UI changed " + visibility);
134+
135+
fullScreen = (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0;
136+
109137
// show/hide action bar according to full-screen mode
110-
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) != 0) {
138+
if (fullScreen) {
111139
CanvasActivity.this.getSupportActionBar().hide();
112-
fullScreen = true;
113140
Toast.makeText(CanvasActivity.this, "Press Back button to leave full-screen mode.", Toast.LENGTH_LONG).show();
114141
} else
115142
CanvasActivity.this.getSupportActionBar().show();
116143
}
117144

118145

146+
// template image logic
147+
119148
private String getTemplateImagePath() {
120149
return preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null);
121150
}
@@ -143,6 +172,7 @@ public void clearTemplateImage(MenuItem item) {
143172

144173
@Override
145174
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
175+
super.onActivityResult(requestCode, resultCode, data);
146176
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
147177
Uri selectedImage = data.getData();
148178
String[] filePathColumn = { MediaStore.Images.Media.DATA };
@@ -159,32 +189,41 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
159189
} finally {
160190
cursor.close();
161191
}
162-
} else
163-
super.onActivityResult(requestCode, resultCode, data);
192+
}
164193
}
165194

166195
public void showTemplateImage() {
167-
String picturePath = preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null);
168-
if (picturePath != null) {
169-
Drawable drawable = BitmapDrawable.createFromPath(picturePath);
170-
getWindow().setBackgroundDrawable(drawable);
171-
} else
172-
getWindow().setBackgroundDrawableResource(android.R.drawable.screen_background_light);
196+
ImageView template = (ImageView)findViewById(R.id.canvas_template);
197+
template.setImageDrawable(null);
198+
199+
if (template.getVisibility() == View.VISIBLE) {
200+
String picturePath = preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null);
201+
if (picturePath != null)
202+
try {
203+
// TODO load bitmap efficiently, for intended view size and display resolution
204+
// https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
205+
final Drawable drawable = new BitmapDrawable(getResources(), picturePath);
206+
template.setImageDrawable(drawable);
207+
} catch (Exception e) {
208+
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
209+
}
210+
}
173211
}
174212

175213

176214
private class ConfigureNetworkingTask extends AsyncTask<Void, Void, Boolean> {
177215
@Override
178216
protected Boolean doInBackground(Void... params) {
179-
return netClient.configureNetworking();
217+
return netClient.reconfigureNetworking();
180218
}
181219

182220
protected void onPostExecute(Boolean success) {
183-
if (success) {
184-
setContentView(canvas);
221+
if (success)
185222
Toast.makeText(CanvasActivity.this, "Touch events will be sent to " + netClient.destAddress.getHostAddress() + ":" + NetworkClient.GFXTABLET_PORT, Toast.LENGTH_LONG).show();
186-
} else
187-
setContentView(R.layout.activity_no_host);
223+
224+
findViewById(R.id.canvas_template).setVisibility(success ? View.VISIBLE : View.GONE);
225+
findViewById(R.id.canvas).setVisibility(success ? View.VISIBLE : View.GONE);
226+
findViewById(R.id.canvas_message).setVisibility(success ? View.GONE : View.VISIBLE);
188227
}
189228
}
190229

app-android/app/src/main/java/at/bitfire/gfxtablet/CanvasView.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.SharedPreferences;
66
import android.graphics.Color;
77
import android.preference.PreferenceManager;
8+
import android.support.annotation.NonNull;
89
import android.util.AttributeSet;
910
import android.util.Log;
1011
import android.view.MotionEvent;
@@ -13,32 +14,63 @@
1314
import at.bitfire.gfxtablet.NetEvent.Type;
1415

1516
@SuppressLint("ViewConstructor")
16-
public class CanvasView extends View {
17+
public class CanvasView extends View implements SharedPreferences.OnSharedPreferenceChangeListener {
1718
private static final String TAG = "GfxTablet.CanvasView";
1819

1920
final SharedPreferences settings;
20-
final NetworkClient netClient;
21+
NetworkClient netClient;
2122
boolean acceptStylusOnly;
2223
int maxX, maxY;
2324

24-
public CanvasView(Context context, NetworkClient networkClient) {
25-
super(context);
26-
this.netClient = networkClient;
2725

28-
// process preferences
26+
// setup
27+
28+
public CanvasView(Context context, AttributeSet attributeSet) {
29+
super(context, attributeSet);
30+
31+
// view is disabled until a network client is set
32+
setEnabled(false);
33+
2934
settings = PreferenceManager.getDefaultSharedPreferences(context);
30-
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
35+
settings.registerOnSharedPreferenceChangeListener(this);
36+
setBackground();
37+
setInputMethods();
3138
}
3239

33-
@Override
34-
protected void onAttachedToWindow() {
35-
super.onAttachedToWindow();
40+
public void setNetworkClient(NetworkClient networkClient) {
41+
netClient = networkClient;
42+
setEnabled(true);
43+
}
44+
45+
46+
// settings
47+
48+
protected void setBackground() {
3649
if (settings.getBoolean(SettingsActivity.KEY_DARK_CANVAS, false))
3750
setBackgroundColor(Color.BLACK);
3851
else
3952
setBackgroundResource(R.drawable.bg_grid_pattern);
4053
}
4154

55+
protected void setInputMethods() {
56+
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
57+
}
58+
59+
@Override
60+
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
61+
switch (key) {
62+
case SettingsActivity.KEY_PREF_STYLUS_ONLY:
63+
setInputMethods();
64+
break;
65+
case SettingsActivity.KEY_DARK_CANVAS:
66+
setBackground();
67+
break;
68+
}
69+
}
70+
71+
72+
// drawing
73+
4274
@Override
4375
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
4476
Log.i(TAG, "Canvas size changed: " + w + "x" + h + " (before: " + oldw + "x" + oldh + ")");
@@ -65,7 +97,7 @@ public boolean onGenericMotionEvent(MotionEvent event) {
6597
}
6698

6799
@Override
68-
public boolean onTouchEvent(MotionEvent event) {
100+
public boolean onTouchEvent(@NonNull MotionEvent event) {
69101
if (isEnabled()) {
70102
for (int ptr = 0; ptr < event.getPointerCount(); ptr++)
71103
if (!acceptStylusOnly || (event.getToolType(ptr) == MotionEvent.TOOL_TYPE_STYLUS)) {
@@ -104,4 +136,5 @@ short normalizeY(float x) {
104136
short normalizePressure(float x) {
105137
return (short)(Math.min(Math.max(0, x), 2.0) * Short.MAX_VALUE/2.0);
106138
}
139+
107140
}

app-android/app/src/main/java/at/bitfire/gfxtablet/NetworkClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313

1414

1515
public class NetworkClient implements Runnable {
16-
static int GFXTABLET_PORT = 40118;
16+
static final int GFXTABLET_PORT = 40118;
1717

18-
LinkedBlockingQueue<NetEvent> motionQueue = new LinkedBlockingQueue<>();
18+
final LinkedBlockingQueue<NetEvent> motionQueue = new LinkedBlockingQueue<>();
1919
LinkedBlockingQueue<NetEvent> getQueue() { return motionQueue; }
2020

2121
InetAddress destAddress;
22-
SharedPreferences preferences;
22+
final SharedPreferences preferences;
2323

2424
NetworkClient(SharedPreferences preferences) {
2525
this.preferences = preferences;
2626
}
2727

28-
boolean configureNetworking() {
28+
boolean reconfigureNetworking() {
2929
try {
3030
String hostName = preferences.getString(SettingsActivity.KEY_PREF_HOST, "unknown.invalid");
3131
destAddress = InetAddress.getByName(hostName);

app-android/app/src/main/java/at/bitfire/gfxtablet/SettingsActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package at.bitfire.gfxtablet;
22

3-
import android.app.Activity;
43
import android.os.Bundle;
54
import android.support.v7.app.ActionBarActivity;
5+
import android.support.v7.app.AppCompatActivity;
66

7-
public class SettingsActivity extends ActionBarActivity {
7+
public class SettingsActivity extends AppCompatActivity {
88
public static final String
99
KEY_PREF_HOST = "host_preference",
1010
KEY_PREF_STYLUS_ONLY = "stylus_only_preference",
771 Bytes
Loading
147 Bytes
Loading
232 Bytes
Loading
385 Bytes
Loading

0 commit comments

Comments
 (0)