トラッキング コード

3/01/2015

How to check Toast window, on android test-kit Espresso

If we read the following public document, we can check other window.
Using inRoot to target non-default windows

onView(withText("South China Sea"))
  .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
  .perform(click());

But, I think this is not cool.

We can create a custom matcher.
Toast window has the WindowManager.LayoutParams.TYPE_TOAST.

The followinf code is sample.
    /**
     * Matcher that is Toast window.
     */
    public static Matcher<Root> isToast() {
        return new TypeSafeMatcher<Root>() {

            @Override
            public void describeTo(Description description) {
                description.appendText("is toast");
            }

            @Override
            public boolean matchesSafely(Root root) {
                int type = root.getWindowLayoutParams().get().type;
                if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
                    IBinder windowToken = root.getDecorView().getWindowToken();
                    IBinder appToken = root.getDecorView().getApplicationWindowToken();
                    if (windowToken == appToken) {
                        // windowToken == appToken means this window isn't contained by any other windows.
                        // if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
                        return true;
                    }
                }
                return false;
            }
        };
    }

2/09/2015

How to use Code Coverage at Android Studio

How to set the Code Coverage in build.gradle

We need to add "testCoverageEnabled". The following build.gradle is sample.

android {
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }

+        debug {
+            testCoverageEnabled = true
+        }
    }
}

How to excute Code Coverage

We need to excute Code Coverage from terminal.

./gradlew createDebugCoverageReport

When excute Code Coverage, automatically Gradle run the androidTest.
If not connected device(or Emulator), you shold show the following error.

:app:connectedAndroidTest FAILED          
              
FAILURE: Build failed with an exception.
              
* What went wrong:
Execution failed for task ':app:connectedAndroidTest'.
> com.android.builder.testing.api.DeviceException: java.lang.RuntimeException: No connected devices!

We need to connect device or wake up Emulator.

How to show the Code Covoerage Report

If successed createDebugCoverageReport、Gradle saved report in the following path.
[PROJECT]/app/build/outputs/reports/coverage/debug/index.html

We can investigate the none test Line.




1/21/2015

IllegalStateException at ActivityUnitTestCase with ActionBarActivity

I need to do ActivityUnitTestCase which activity is extends ActionBarActivity.
However, I get the following exception.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)


If call startActivity, Context does not have Theme.AppCompat...
So, We need to call setActivityContext() due to set Context with theme.
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Context context = getInstrumentation().getTargetContext();
        ContextThemeWrapper contextTheme = new ContextThemeWrapper(context, R.style.AppTheme);
        setActivityContext(contextTheme);
        startActivity(new Intent(), null, null);
    }

1/17/2015

How to add Volley Request with parameter of POST and Headers

If need to add parameter of POST and Headers, you need to read the following source code.
There are getHeaders() and getParams() in Request class which is super class of StringRequest class.
     /**
     * Returns a list of extra HTTP headers to go along with this request. Can
     * throw {@link AuthFailureError} as authentication may be required to
     * provide these values.
     * @throws AuthFailureError In the event of auth failure
     */
    public Map<String, String> getHeaders() throws AuthFailureError {
        return Collections.emptyMap();
    }

     /**
     * Returns a Map of parameters to be used for a POST or PUT request.  Can throw
     * {@link AuthFailureError} as authentication may be required to provide these values.
     *
     * <p>Note that you can directly override {@link #getBody()} for custom data.</p>
     *
     * @throws AuthFailureError in the event of auth failure
     */
    protected Map<String, String> getParams() throws AuthFailureError {
        return null;
    }

We can add parameter when do override getHeaders() and getParams() method.The following source is sample.


    /**
     * Request(StringRequest) with params
     *
     * @param url      request url
     * @param listener listener for Response or Error
     * @param params   value of setting Http Params
     * @param headers  value of setting Http headers
     */
    public void get(String url, final ResponseListener listener, final Map<String, String> params
            , final Map<String, String> headers) {
 
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        listener.onResponse(s);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        listener.onErrorResponse(volleyError);
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
 
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return headers;
            }
        };
 
        mRequestQueue.add(request);
    }

7/27/2014

How to handle to auto flip with ViewFlipper

ViewFlipper do not have a handler to auto flipping.
If do handing, we should get Animation Class via VewFlipper#getInAnimation().
We register to Listener.


The follow code is sample.
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_flashing_card, null);
        mVewFlipper = (ViewFlipper) view.findViewById(R.id.viewFlipper1);

        mVewFlipper.setAutoStart(true);
        mVewFlipper.setInAnimation(getActivity(), android.R.anim.slide_in_left);
        mVewFlipper.getInAnimation().setAnimationListener(this);
        mVewFlipper.setFlipInterval(2000);

        return view;
    }

    @Override
    public void onAnimationEnd(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }

3/30/2014

How to use SwipeRefreshLayout, like a twitter, facebook



Create the Layout

You need to use the android.support.v4.widget.SwipeRefreshLayout.
Content is included SwipeRefreshLayout for clild layout( or view).

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!-- some full screen pullable view that will be the offsetable content -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content"/>
</android.support.v4.widget.SwipeRefreshLayout>


Update for Swipe

You need the following step, due to updating for Swipe.
1. call setOnRefreshListener methiod due to setting Listener which handles the update starting.
2. onRefresh() is handled the update starting.
3. Then your update process finish、call setRefreshing(false).
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_swipe_refresh_widget);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    public void onRefresh() {
        refresh();
    }

    private void refresh() {
        Message msg = mHander.obtainMessage(0, this);
        mHander.sendMessageDelayed(msg, 1000);
    }

    private static Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            SwipeRefreshLayoutActivity actibity = (SwipeRefreshLayoutActivity) msg.obj;
            actibity.mSwipeRefreshLayout.setRefreshing(false);
        }
    };

3/15/2014

How to use Google Play services

Ready to use Google Play services

If you need to use Google Play services API, you should do download of Google Play services on Android SDK Manager.Google Play services was saved in following directory.<AndroidSDK>\sdk\extras\google\google_play_services\libproject\google-play-services_lib

Importing this Android Project to eclipse.

You must write a meta-data in AndroidMaifest.xml using Google Play services API.

    <application
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

Check to exist of Google Play services

You shoud Check to exist of Google Play services in a device.

public class UsingGooglePlayServiceSampleActivity extends FragmentActivity {

    private boolean isGooglePlayServicesAvailable() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if (ConnectionResult.SUCCESS == resultCode) {
            return true;
        } else {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    CONNECTION_FAILURE_RESOLUTION_REQUEST);
            if (dialog != null) {
                ErrorDialogFragment errorFragment = new ErrorDialogFragment();
                errorFragment.setDialog(dialog);
                errorFragment.show(getSupportFragmentManager(), "errro_dialog");
            }
        }

        return false;
    }

    public static class ErrorDialogFragment extends DialogFragment {

        private Dialog mDialog;

        public ErrorDialogFragment() {
            super();
            mDialog = null;
        }

        public void setDialog(Dialog dialog) {
            mDialog = dialog;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return mDialog;
        }

        @Override
        public void onDestroy() {
            mDialog = null;
            super.onDestroy();
        }
    }
}

Calling a method of GooglePlayServicesUtil.isGooglePlayServicesAvailable(), you can get Result code.If Result code is error, you can get a Dialog Object from calling method GooglePlayServicesUtil.getErrorDialog().You must use a DialogFragment to show a dialog.

If User close a dialog, onActivityResult is called with Result Code.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CONNECTION_FAILURE_RESOLUTION_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                // retry process
            }
        }
    }