Categories
discuss

Change the device ID on an Android emulator?

Is there a way to change the IMEI that will be returned by the emulator’s TelephonyManager? Also, is there a way to change the ID returned by Settings.Secure.ANDROID_ID?

I use these IDs to distinguish my users from one another when storing their data on the server side. It would be nice if my QA team could change these IDs so that they are not all using the same set of user data.

Answer

I haven’t tried it, but this page outlines a method that involves manually modifying the emaultor.exe file. It seems pretty straightforward, though you’d have to create a separate emulator for each QA team member.

Categories
discuss

Resize HTML elements on Android orientation change

I am trying to bind an event in Javascript to either the orientationchange or resize events in Android in order to change the width/height of some elements for my web app. In the event, I use window.innerHeight and window.innerWidth to get the current height and width of the window.

This works great on iOS and desktop devices, but on Android it seems that it calls this event before changing the values in the window variable. Therefore, when someone switches from portrait to landscape, I still get the values for portrait and therefore cannot resize correctly. Does anyone know what the problem is, and how I can fix it?

Answer

I have fixed the problem. It turns out that the resize event works correctly, but the orientationchange event does not. I was testing to see if the orientationevent was present, and then binding to that if possible. I have changed my code over to only use the resize event, and have achieved the desired effect.

I’m not sure if this is unique to my code or not, but that’s what fixed it for me.

Categories
discuss

EasyMock returns Null for Expected Method

I have am having a problem with EasyMock returning null for an expected (defined) method call.

Creation of the mocked Object

mock = EasyMock.createMock(DAO.class);

Mock Set up in unit test.

expect(mock.update(myObj).andReturn(myObjUpdated).once();
replayAll();
service.setDao(mock);
service.processData(myObj);
verifyAll();

processData method simply calls

MyObject objUpdated = dao.update(myObj);

here is the interface that the mock is being built from.

public interface DAO {
   public <ENTITY> ENTITY update(ENTITY entity);
}

I am pretty confused by what might be causing the problem. I have confirmed that ‘obj’ is the same object as I defined in the unit test. I have also not experienced this problem (that I am aware of) with any other methods that mocked.

Could the problem possibly be with the Object that is being passed in?

Thanks in advance. I am really not sure what other information might be helpful to you here.

edit: this is the test class (and as it turns out where my misunderstanding began)

public class TestMyService extends EasyMockHelper {...}

Answer

So it turns out that my main problem isn’t with the expectations or even with the creation of the mock object. I had a fundamental misunderstanding about how the EasyMockSupport class which my test is extending functions. This isn’t covered very well in the documentation, but if you exam the examples a bit more closely my error became obvious.

The EasyMockSupport class gives my test class access to methods such as replayAll(), verifyAll(), and resetAll(). what these do is allow me to now worry about manually controlling each created mock object. However, what the documentation failed to mention was that you have to create you Mock object USING the methods provided by the EasyMockSupport class so that it can properly register the controls. ((this makes total sense btw, I simply wasn’t reading it anywhere)). The EasyMockSupport class if you look into the API provides the child class with all the methods that it would normally use statically from the EasyMock class, such as createMock(Class class).

So as for the updated code

public class TestMyService extends EasyMockSupport {
   private MyService service;
   private MyDao dao;

   private MyObject myObj;

   @Before public void setUp() {
      service = new MyService();

      // THIS IS THE KEY
      mock = createMock(IDao.class); //CORRECT
      // mock = EasyMock.createMock(IDao.class); //WRONG

      service.setDao(mock);
      myObj = new MyObject("expectedData");
   }
   @After public void tearDown() {
      verifyAll();
   }
   @Test public void testMyService() {
      expect(mock.update(myObj)).andReturn(myObj);
      replayAll();
      service.myService(myObj);
   }
}

public class MyService() {
   private IDao dao;
   public void setDao(IDao dao) {this.dao = dao; }
   public MyObject myService(MyObject myObj) {
      return dao.update(myObj);
   }
}
Categories
discuss

powershell run java process problem

I’m trying to run a java process via Powershell in Windows XP. Here’s the command:

java.exe -cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install

So, the classpath is . and .common.jar (I think java takes the wrong slashes, right?) There are two environment variables, one “atest1” the other “.” and the class to execute main on is DW_Install (in the default package).

This command works in cmd.exe, but doesn’t is PS. What’s going on? What is PS doing while parsing this command that CMD doesn’t do (or vice versa)?

Aaron

Answer

The problem is that PS for some reason parses -Dresourcepath=. differently than cmd. What works is

java -cp '.;.common.jar' -Dcontext=atest1 "-Dresourcepath=." DW_Install

It doesn’t matter which way the slash goes, and it doesn’t matter which quotes one uses (' or "). The classpath must be escaped, however, with some kind of quotes. A good test to see what’s getting by the PS interpreter is to echo it. The following:

echo java -cp '.;.common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install

yields the following output:

java
-cp
.;.common.jar
-Dcontext=atest1
-Dresourcepath=
.
DW_Install

(Notice the resourcepath and the value of resourcepath are not on the same line.) Whereas the output to

echo java -cp '.;.common.jar' -Dcontext=atest1 '-Dresourcepath=.' DW_Install

yields the following output:

java
-cp
.;.common.jar
-Dcontext=etaste1
-Dresourcepath=.
DW_Install

Which is much more to our liking.

Although I wish this upon none of you, I hope that this post helps those of you that must deploy java projects on Windows machines (even though they will not run on any other platform ever).

Categories
discuss

Trouble with detecting gestures over ListView

I have an Activity that contains a ViewFlipper. The ViewFlipper includes 2 layouts, both of which are essentially just ListViews.

So the idea here is that I have two lists and to navigate from one to the other I would use a horizontal swipe.

I have that working. However, what ever list item your finger is on when the swipe begins executing, that item will also be long-clicked.

Here is the relevant code I have:

public class MyActivity extends Activity implements OnItemClickListener, OnClickListener {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentScreen != SCREEN_SECONDLIST) {
                        mCurrentScreen = SCREEN_SECONDLIST;
                        mFlipper.setInAnimation(inFromRightAnimation());
                        mFlipper.setOutAnimation(outToLeftAnimation());
                        mFlipper.showNext();
                        updateNavigationBar();
                    }
                }
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentScreen != SCREEN_FIRSTLIST) {
                        mCurrentScreen = SCREEN_FIRSTLIST;
                        mFlipper.setInAnimation(inFromLeftAnimation());
                        mFlipper.setOutAnimation(outToRightAnimation());
                        mFlipper.showPrevious();
                        updateNavigationBar();
                    }
                }
            } catch (Exception e) {
                // nothing
            }
            return true;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }





    ViewFlipper mFlipper;

    private int mCurrentScreen = SCREEN_FIRSTLIST;

    private ListView mList1;
    private ListView mList2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_flipper);

        mFlipper = (ViewFlipper) findViewById(R.id.flipper);

        mGestureDetector = new GestureDetector(new MyGestureDetector());
        mGestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (mGestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };

        // set up List1 screen

        mList1List = (ListView)findViewById(R.id.list1);
        mList1List.setOnItemClickListener(this);
        mList1List.setOnTouchListener(mGestureListener);

        // set up List2 screen
        mList2List = (ListView)findViewById(R.id.list2);
        mList2List.setOnItemClickListener(this);
        mList2List.setOnTouchListener(mGestureListener);

    }

    …
}

If I change the “return true;” statement from the GestureDetector to “return false;”, I do not get long-clicks. Unfortunately, I get regular clicks.

Does anyone know how I can get around this?

Answer

Just to throw in a completely different answer with a completely different approach…

I’ve made a custom view called SwipeView, it’s open source etc etc blah blah blah. It should let you do what you want to do as simply as going:

mSwipeView.addChild(myList1);
mSwipeView.addChild(myList2);

You won’t have to mess about with gesture detectors or anything, and the the swipe should follow your finger as you do it…

(This reads like a terrible terrible advert, please excuse me. It’s been a long day 😉

Have some wonderful links:

http://jasonfry.co.uk/?id=23

http://jasonfry.co.uk/?id=24

http://jasonfry.co.uk/?id=28

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..