Skip to content
This repository has been archived by the owner on Nov 11, 2021. It is now read-only.

Commit

Permalink
Change template image logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Jul 1, 2015
1 parent fb20c3f commit f64df04
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 89 deletions.
2 changes: 1 addition & 1 deletion app-android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ android {
}

dependencies {
compile "com.android.support:appcompat-v7:21.0.+"
compile "com.android.support:appcompat-v7:22.+"
}
7 changes: 3 additions & 4 deletions app-android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
android:versionCode="4"
android:versionName="1.3" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<uses-sdk />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Expand All @@ -18,7 +16,8 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme"
android:label="@string/app_name" >
android:label="@string/app_name"
android:largeHeap="true">
<activity
android:name="at.bitfire.gfxtablet.CanvasActivity"
android:label="@string/app_name"
Expand Down
101 changes: 70 additions & 31 deletions app-android/app/src/main/java/at/bitfire/gfxtablet/CanvasActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

public class CanvasActivity extends ActionBarActivity implements View.OnSystemUiVisibilityChangeListener {
private static int RESULT_LOAD_IMAGE = 1;
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";

final Uri homepageUri = Uri.parse(("https://rfc2822.github.io/GfxTablet/"));

NetworkClient netClient;
CanvasView canvas;

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

preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.registerOnSharedPreferenceChangeListener(this);

setContentView(R.layout.activity_canvas);

// create network client in a separate thread
netClient = new NetworkClient(PreferenceManager.getDefaultSharedPreferences(this));
new Thread(netClient).start();

canvas = new CanvasView(CanvasActivity.this, netClient);
}

@Override
protected void onStart() {
super.onStart();

new ConfigureNetworkingTask().execute();

// notify CanvasView of the network client
CanvasView canvas = (CanvasView)findViewById(R.id.canvas);
canvas.setNetworkClient(netClient);
}

@Override
Expand All @@ -61,9 +66,9 @@ protected void onResume() {
}

@Override
protected void onStop() {
protected void onDestroy() {
super.onDestroy();
netClient.getQueue().add(new NetEvent(NetEvent.Type.TYPE_DISCONNECT));
super.onStop();
}

@Override
Expand All @@ -81,14 +86,33 @@ public void onBackPressed() {
}

public void showAbout(MenuItem item) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(("https://rfc2822.github.io/GfxTablet/"))));
startActivity(new Intent(Intent.ACTION_VIEW, homepageUri));
}

public void showDonate(MenuItem item) {
startActivity(new Intent(Intent.ACTION_VIEW, homepageUri.buildUpon().fragment("donate").build()));
}

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


// preferences were changed

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case SettingsActivity.KEY_PREF_HOST:
Log.i(TAG, "Recipient host changed, reconfiguring network client");
new ConfigureNetworkingTask().execute();
break;
}
}


// full-screen methods

public void switchFullScreen(MenuItem item) {
final View decorView = getWindow().getDecorView();
int uiFlags = decorView.getSystemUiVisibility();
Expand All @@ -97,7 +121,7 @@ public void switchFullScreen(MenuItem item) {
uiFlags ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
if (Build.VERSION.SDK_INT >= 16)
uiFlags ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
if (Build.VERSION.SDK_INT >= 18)
if (Build.VERSION.SDK_INT >= 19)
uiFlags ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

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

@Override
public void onSystemUiVisibilityChange(int visibility) {
Log.i("GfxTablet", "System UI changed " + visibility);

fullScreen = (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0;

// show/hide action bar according to full-screen mode
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) != 0) {
if (fullScreen) {
CanvasActivity.this.getSupportActionBar().hide();
fullScreen = true;
Toast.makeText(CanvasActivity.this, "Press Back button to leave full-screen mode.", Toast.LENGTH_LONG).show();
} else
CanvasActivity.this.getSupportActionBar().show();
}


// template image logic

private String getTemplateImagePath() {
return preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null);
}
Expand Down Expand Up @@ -143,6 +172,7 @@ public void clearTemplateImage(MenuItem item) {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Expand All @@ -159,32 +189,41 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
} finally {
cursor.close();
}
} else
super.onActivityResult(requestCode, resultCode, data);
}
}

public void showTemplateImage() {
String picturePath = preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null);
if (picturePath != null) {
Drawable drawable = BitmapDrawable.createFromPath(picturePath);
getWindow().setBackgroundDrawable(drawable);
} else
getWindow().setBackgroundDrawableResource(android.R.drawable.screen_background_light);
ImageView template = (ImageView)findViewById(R.id.canvas_template);
template.setImageDrawable(null);

if (template.getVisibility() == View.VISIBLE) {
String picturePath = preferences.getString(SettingsActivity.KEY_TEMPLATE_IMAGE, null);
if (picturePath != null)
try {
// TODO load bitmap efficiently, for intended view size and display resolution
// https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
final Drawable drawable = new BitmapDrawable(getResources(), picturePath);
template.setImageDrawable(drawable);
} catch (Exception e) {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
}
}


private class ConfigureNetworkingTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
return netClient.configureNetworking();
return netClient.reconfigureNetworking();
}

protected void onPostExecute(Boolean success) {
if (success) {
setContentView(canvas);
if (success)
Toast.makeText(CanvasActivity.this, "Touch events will be sent to " + netClient.destAddress.getHostAddress() + ":" + NetworkClient.GFXTABLET_PORT, Toast.LENGTH_LONG).show();
} else
setContentView(R.layout.activity_no_host);

findViewById(R.id.canvas_template).setVisibility(success ? View.VISIBLE : View.GONE);
findViewById(R.id.canvas).setVisibility(success ? View.VISIBLE : View.GONE);
findViewById(R.id.canvas_message).setVisibility(success ? View.GONE : View.VISIBLE);
}
}

Expand Down
55 changes: 44 additions & 11 deletions app-android/app/src/main/java/at/bitfire/gfxtablet/CanvasView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
Expand All @@ -13,32 +14,63 @@
import at.bitfire.gfxtablet.NetEvent.Type;

@SuppressLint("ViewConstructor")
public class CanvasView extends View {
public class CanvasView extends View implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "GfxTablet.CanvasView";

final SharedPreferences settings;
final NetworkClient netClient;
NetworkClient netClient;
boolean acceptStylusOnly;
int maxX, maxY;

public CanvasView(Context context, NetworkClient networkClient) {
super(context);
this.netClient = networkClient;

// process preferences
// setup

public CanvasView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);

// view is disabled until a network client is set
setEnabled(false);

settings = PreferenceManager.getDefaultSharedPreferences(context);
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
settings.registerOnSharedPreferenceChangeListener(this);
setBackground();
setInputMethods();
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
public void setNetworkClient(NetworkClient networkClient) {
netClient = networkClient;
setEnabled(true);
}


// settings

protected void setBackground() {
if (settings.getBoolean(SettingsActivity.KEY_DARK_CANVAS, false))
setBackgroundColor(Color.BLACK);
else
setBackgroundResource(R.drawable.bg_grid_pattern);
}

protected void setInputMethods() {
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case SettingsActivity.KEY_PREF_STYLUS_ONLY:
setInputMethods();
break;
case SettingsActivity.KEY_DARK_CANVAS:
setBackground();
break;
}
}


// drawing

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Log.i(TAG, "Canvas size changed: " + w + "x" + h + " (before: " + oldw + "x" + oldh + ")");
Expand All @@ -65,7 +97,7 @@ public boolean onGenericMotionEvent(MotionEvent event) {
}

@Override
public boolean onTouchEvent(MotionEvent event) {
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (isEnabled()) {
for (int ptr = 0; ptr < event.getPointerCount(); ptr++)
if (!acceptStylusOnly || (event.getToolType(ptr) == MotionEvent.TOOL_TYPE_STYLUS)) {
Expand Down Expand Up @@ -104,4 +136,5 @@ short normalizeY(float x) {
short normalizePressure(float x) {
return (short)(Math.min(Math.max(0, x), 2.0) * Short.MAX_VALUE/2.0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@


public class NetworkClient implements Runnable {
static int GFXTABLET_PORT = 40118;
static final int GFXTABLET_PORT = 40118;

LinkedBlockingQueue<NetEvent> motionQueue = new LinkedBlockingQueue<>();
final LinkedBlockingQueue<NetEvent> motionQueue = new LinkedBlockingQueue<>();
LinkedBlockingQueue<NetEvent> getQueue() { return motionQueue; }

InetAddress destAddress;
SharedPreferences preferences;
final SharedPreferences preferences;

NetworkClient(SharedPreferences preferences) {
this.preferences = preferences;
}

boolean configureNetworking() {
boolean reconfigureNetworking() {
try {
String hostName = preferences.getString(SettingsActivity.KEY_PREF_HOST, "unknown.invalid");
destAddress = InetAddress.getByName(hostName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package at.bitfire.gfxtablet;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;

public class SettingsActivity extends ActionBarActivity {
public class SettingsActivity extends AppCompatActivity {
public static final String
KEY_PREF_HOST = "host_preference",
KEY_PREF_STYLUS_ONLY = "stylus_only_preference",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app-android/app/src/main/res/drawable-mdpi/ic_action_picture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app-android/app/src/main/res/drawable-xhdpi/ic_action_picture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app-android/app/src/main/res/drawable-xxhdpi/ic_action_picture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions app-android/app/src/main/res/layout/activity_canvas.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:id="@+id/canvas_template"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"/>

<view
android:id="@+id/canvas"
class="at.bitfire.gfxtablet.CanvasView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/canvas_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="@string/no_host_defined"
android:textAppearance="?android:attr/textAppearanceLarge"/>

</FrameLayout>
Loading

0 comments on commit f64df04

Please sign in to comment.