Capturing Multitouch in Android - android

For my games I allways use singletouch so I allways keep track of the Pointer in an onTouch() Method like this:
-get the pointer Id at (0) with event.getPointerId(0); when pId==0, where pId is the pointer Id
-there will be no computation of other pointers when the pId!=-1(standard value I assign when the prime pointer is lifted up again.) and if the current pointer index is not the same as the pointer indes of the pointer Id I have saved in pId
like so:
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(pID!=-1)break;
...
pID=e.getPointerId(0);
...
break;
case MotionEvent.ACTION_MOVE:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
pID=-1;
...
break;
}
But now I stumbled across a code that I wrote 2 weeks ago where I do it other way, because I wanted to react to Multitouch(maximum two touch pointers allowed for zooming):
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(tP1.getPointerIndex()==-1&&tP2.getPointerIndex()==-1)
{
tP1.setPointerIndex(currentPointerIndex);
mode=Mode.SINGLE;
}
else if(tP1.getPointerIndex()!=-1&&tP2.getPointerIndex()==-1&&!isDragging)
{
tP2.setPointerIndex(currentPointerIndex);
mode=Mode.MULTI;
}
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
tP1.oldX=tP1.X;
tP1.oldY=tP1.Y;
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
tP2.oldX=tP2.X;
tP2.oldY=tP2.Y;
}
else break;
...
break;
case MotionEvent.ACTION_MOVE:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
if(mode==Mode.SINGLE)
{
mode=Mode.NONE;
tP1.resetPointerIndex();
}
else if(mode==Mode.MULTI)
{
mode=Mode.SINGLE;
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.resetPointerIndex();
tP1.setPointerIndex(tP2.getPointerIndex());
tP1.X=tP2.X;
tP1.Y=tP2.Y;
tP1.oldX=tP2.oldX;
tP1.oldY=tP2.oldY;
tP2.resetPointerIndex();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.resetPointerIndex();
}
}
break;
}
tP1 and tP2 ar class objects who keep track of the last x and y coordinates and of the pointer index. So they represent the two pointers. I allways swap the second pointer on the first pointer if the first pointer is raised up and the second is still on. Now it is really embarrassing because I wrote this code for a colleague of mine for a few bucks.
So I jsut keep track of the indices of the pointers but they are sometimes changing. I didnt noticed it because everything was working fine. But now as I came accross it I noticed this. What should I do. Do I need to change it or do you see no problem to let this code be as it is?
I am just assuming that it can go wrong because I dont track the pointer Id but only the pointer Index.
Also I never experience it when testing that for example the primary pointer when first touched get swapped to another index but 0 when another touchpointer comes down.
I feel so dumb that I didnt notice it, so now I need a good advice how to behave pls help...
It is really strange that is code worked because I do this:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
So Android not only assigns the index of the first pointer with 0 but also the pointerId with 0. This code is a mess i think

Related

How to execute two if conditions in android

I want to execute two if conditions either first if condition or second if condition based on my requirement.
Now below code if it executing second if condition everytime
switch (specilaity_name) {
case "Cardiac":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
break;
case "Urology":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_urology__1___1_);
specialization.setTextColor(Color.WHITE);
break;
case "Gynaecology":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_group_2105);
specialization.setTextColor(Color.WHITE);
break;
}
Well. You're using nested if else in the else part of outer if for each block of code.
If only code in second structure is working, then that's the only condition which holds true during execution of your App.
if(specilaity_name.equalsIgnoreCase("Cardiac")){
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
}else if(specilaity_name.equalsIgnoreCase("Urology")) {
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_white);
speclialistimage.setImageResource(R.drawable.ic_urology__1_);
specialization.setTextColor(Color.rgb(73,179,206));
}else {
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_white);
speclialistimage.setImageResource(R.drawable.ic_group_2105);
specialization.setTextColor(Color.rgb(73, 179, 206));
}
String specialityName = specilaity_name.toUpperCase();
switch (specialityName) {
case "CARDIAC":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
break;
case "UROLOGY":
// add code for urology
break;
case "GYNAECOLOGY":
// add code for Gynaecology
break;
default:
break;
}
}

Periodic location report of device using flutter

I need to report the current location of the device using a flutter app. I need to do that continiuesly even when the app is closed.
I currently implemented it using background_fetch which does the job about every 15 minutes.
It works well when the app is open or minimized. But when the app is closed, it functions in Headless mode and doesn't work. the Exception is:
MissingPluginException(No implementation found for method getLocation on channel lyokone/location)
It seems that in Headless mode, not all the app is loaded in memory. I don't have any idea how to solve it.
Also I tried using an Isolate but I face with a new exception:
native function 'Window_sendPlatformMessage' (4 arguments) cannot be found.
Anybody knows how to solve these problems or have any new idea to do the location tracking?
Another option is use package https://github.com/Lyokone/flutterlocation https://github.com/Lyokone/flutterlocation/wiki/Background-Location-Updates
Support To get location updates even your app is closed, https://github.com/Lyokone/flutterlocation/wiki/Background-Location-Updates might have bugs
Also from transistorsoft and provide Android Headless Mod but need license
transistorsoft package https://github.com/transistorsoft/flutter_background_geolocation/wiki/Android-Headless-Mode
Android Headless Mod https://github.com/transistorsoft/flutter_background_geolocation/wiki/Android-Headless-Mode
code snippet
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
void headlessTask(bg.HeadlessEvent headlessEvent) async {
print('[BackgroundGeolocation HeadlessTask]: $headlessEvent');
// Implement a 'case' for only those events you're interested in.
switch(headlessEvent.name) {
case bg.Event.TERMINATE:
bg.State state = headlessEvent.event;
print('- State: $state');
break;
case bg.Event.HEARTBEAT:
bg.HeartbeatEvent event = headlessEvent.event;
print('- HeartbeatEvent: $event');
break;
case bg.Event.LOCATION:
bg.Location location = headlessEvent.event;
print('- Location: $location');
break;
case bg.Event.MOTIONCHANGE:
bg.Location location = headlessEvent.event;
print('- Location: $location');
break;
case bg.Event.GEOFENCE:
bg.GeofenceEvent geofenceEvent = headlessEvent.event;
print('- GeofenceEvent: $geofenceEvent');
break;
case bg.Event.GEOFENCESCHANGE:
bg.GeofencesChangeEvent event = headlessEvent.event;
print('- GeofencesChangeEvent: $event');
break;
case bg.Event.SCHEDULE:
bg.State state = headlessEvent.event;
print('- State: $state');
break;
case bg.Event.ACTIVITYCHANGE:
bg.ActivityChangeEvent event = headlessEvent.event;
print('ActivityChangeEvent: $event');
break;
case bg.Event.HTTP:
bg.HttpEvent response = headlessEvent.event;
print('HttpEvent: $response');
break;
case bg.Event.POWERSAVECHANGE:
bool enabled = headlessEvent.event;
print('ProviderChangeEvent: $enabled');
break;
case bg.Event.CONNECTIVITYCHANGE:
bg.ConnectivityChangeEvent event = headlessEvent.event;
print('ConnectivityChangeEvent: $event');
break;
case bg.Event.ENABLEDCHANGE:
bool enabled = headlessEvent.event;
print('EnabledChangeEvent: $enabled');
break;
}
}
void main() {
runApp(HelloWorld());
// Register your headlessTask:
BackgroundGeolocation.registerHeadlessTask(headlessTask);
}

Gesture Swipe Problems in Flash

I'm planning to make a digital magazine with adobe flash cc 2014. I have different scenes for different page and I use Gesture_SWIPE to navigate the magazine.
The problem is, I only write the script in 1 scene but it worked for all the scenes. So I can do the swipe in all scenes.
While I want each scenes have a different action such as only page 1 that can't be swiped to the left, only page 2 can be swiped up and down and etc
I did use the if else in each case, it worked for the swiped left, but not working for the swipe right.
Can you please help me? How can I make this scripts work only for 1 scene?
This is my gesture swipe code:
import flash.events.TransformGestureEvent;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, SwipeHandler);
function SwipeHandler(event:TransformGestureEvent):void
{
switch(event.offsetX)
{
case 1: //swiped right
{
prevScene();
break;
if(this.currentScene.name == "Scene 1") //THIS ONE DIDN'T WORK
{
stop();
}
}
case -1: //swiped left
{
if(this.currentScene.name == "Scene 12") //THIS ONE WORK
{
stop();
}
else
{
nextScene();
break;
}
}
}
}
I had a little trouble understanding your problem and requirements but a few things to continue with:
your first case should probably work in the same way as the 2nd
your breaks won't be always fired so will sometimes flow onto the next case the way your statement were setup.
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, SwipeHandler);
function SwipeHandler(event:TransformGestureEvent):void {
switch(event.offsetX) {
case 1:
if(this.currentScene.name == "Scene 1") {
stop();
} else {
prevScene();
}
break;
case -1:
if(this.currentScene.name == "Scene 12") {
stop();
} else{
nextScene();
}
break;
default:
//something else happened
}
}

android how to change from if-else to switch case

I have an application about pmt function. However there are so many conditions that need to be handled. Somehow the app will not work with having more than 12 if-else. I want to use switch case, but i still not really understand how to use switch case(been 1 and half month since my 1st try using eclipse).Any example will be highly appreciated.
here is my example code:
if(String1.toString().equals("condition1")){
//do something
if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
//do something else
}
}
else if(String1.toString().equals("condition2")){
//do something
if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
//do something else
}
}
if(String1.toString().equals("condition3")){
//do something
if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
//do something else
}
}
and still keep going....to handle all possibilities .I am wondering, How to do this in switch case . Or a better implementation if we have 3 times 3 conditions. For example a,b,c(suppose these three conditions can only be used once) and d,e,f and g,h,i then condition 1 is a,d,g ; condition 2 is a,d,h condition 3 is a,d,i ; condition 4 a,e,g........on so on
Note:Suppose that the API version is 8-11 (old android)
thanks
The answer is dependent on your target version of android. From KitKat and upwards (API Level 19+), Java 7's switch (String) is available. I'd also strongly suggest trying to group the subcases (condition n.x) into different methods. It just gets very unwieldly quickly, otherwise:
switch (String1.toString) {
case "condition1":
handleCase1(String2, String3);
break;
case "condition2":
handleCase2(String2, String3);
break;
}
If that still results in too complex code, you can try a lookup table together with a command pattern:
class ConditionKey {
final String String1;
final String String2;
final String String3;
public int hashCode(); // hash strings
public boolean equals(); // compare strings
}
interface ConditionCommand {
// use whatever arguments the operation needs, you can also
// add fields and initialize in the constructor
void perform(final ConditionKey key, /* [...] */);
}
Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
new ConditionKey("condition1", "condition1.1", "condition1.2"),
new ConditionCommand() {
void perform(final ConditionKey key) {
// perform actions that need to be done
}
}
);
And then instead of the if-else or switch-case:
[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);
since java 1.7 switch on string is supported.
you could annidate two switch:
switch(String1) {
case "condition1": {
switch(String2) {
case "condition1.1":
break;
// ... other cases
default:
break;
}
}
break;
// ... other cases
default break;
}

Swichcase not working in android?

I am stuck in swich case. Please check code below
Log.e("##################### pos",""+posSel_lay);
String ff=Integer.toString(posSel_lay);
//var ffs=Integer.toString(posSel_lay);
Log.e("##################### pos",""+ff);
if(ff.equals("0")){
Log.e("###################Lagan","");
}else if(ff.equals("1")){
Log.e("###################MBBS","");
}else if(ff.equals("2")){
Log.e("###################JODHA","");
}else if(ff.equals("3")){
Log.e("###################ZINDAGI","");
}
I got log only this line
##################### pos 2
& its not going in swich case why i dont know, can you help me please?
All the answers are good.
but you should at least put any message into message param of Log. otherwise you will not able to see this in logs
Log.e("###################ZINDAGI","some message.");
By default a switch case works WITH integers, why don't you try something like the following:
Log.e("##################### pos",""+posSel_lay);
switch (posSel_lay){
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
default:
break;
}
Now instead of converting to a string, and using an if-else chain, you use a select statement, which IMO is much cleaner.
Log.e("##################### pos", "" + posSel_lay );
switch( posSel_lay ) {
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
}
I can't really understand your question , but here is SwitchCase
switch(posSel_lay){
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
}
I found solution. I get this posSel_lay from bundle which is passed by other activity.Previously i written switch code in on button click so now i changed flow,
When i am getting value of posSel_lay from bundle that time only i written code of switch
& there i am making some boolean values true or false which is declared locally. And when user
clicks on button that time i used boolean variables for checking. Then its done.
Thanks for your reply.

Categories

Resources