Warning: Use of undefined constant _FILE_ - assumed '_FILE_' (this will throw an Error in a future version of PHP) in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/ad-blocker-stats-vip/ad-blocker-stats-vip.php on line 13

Archive for the ‘Android’ Category

Setting up Google single sign in

2017-01-16   

This post is more like a note to myself. Setting up Google sign in is not something I do very often. It happens on average once a year and each time I have to do it I have to struggle with the crappy documentation (because I manage to forget the implementation details).

 

So:
1 .Go to Google developer console at https://console.developers.google.com/apis/

2. Create new project, Click 'Credentials' menu item

3. Click the Create credentials button -> OAuth client ID -> Web application -> Create. Don't mind the "Web application" thing, you need this for the android app. Enter some name and then Save. Google will generate two things for it: Client ID and Client secret. Client ID is used in the android app when creating GoogleSignInOptions like this:

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.google_client_id))
                .build();

Client secret is used on the server side only.

4. Now we have to create android credentials for both debug and release (optionally) .

4.1. Debug

Click the Create credentials button -> OAuth client ID -> Android

For name use 'Android client debug' (for example)

For signing-certificate finger print use:

keytool -exportcert -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore

to show the fingerprint (use SHA-1)

For package name use the package name from your AndroidManifest.xml. You may need to add '.debug' suffix if gradle is configured to add it when building debug version.

4.2. Release

Do the same but using the release key

keytool -exportcert -keystore android_release_keys.keystore -list -v

 

4. Go to https://developers.google.com/identity/sign-in/android/start-integrating

Click 'Get a configuration' file button. Select the app and package (usually release package, i.e. without debug). 'Continue and configure services'

There is a button 'Enable google sign in", Click it.

There is a button "Generate conf files". Click it.

There is a button 'Download google-services.json'. DON'T click it.

Go again to https://developers.google.com/identity/sign-in/android/start-integrating
Select the app, but enter the debug package, i.e. with the .debug suffix.'Continue and configure services'

There is a button 'Enable google sign in", Click it.

There is a button "Generate conf files". Click it.

There is a button 'Download google-services.json'. Click it and download the file. This file contains the configuration for both release and debug builds.

Copy the file in the root of the app module folder (that is NOT the project root).

Now everything should work.

Android tools 2014

2014-08-28   

Good practice is to learn from the best. Here is a podcast with Jake Wharton where he mentions the libs and tools he uses.

In short here is the list:

Retrofit: http://square.github.io/retrofit/
Picasso: http://square.github.io/picasso/
Dagger: http://square.github.io/dagger/
OkHttp: http://square.github.io/okhttp/
Butter Knife: http://jakewharton.github.io/butterknife
Robolectric: http://robolectric.org/
Espresso: https://code.google.com/p/android-test-kit/
AssertJ Android: http://square.github.io/assertj-android
One more addition to the Wharton's list is:
Parcelabler (online tool for generating parcelable versions of your classes): http://www.parcelabler.com/

Dynamic ListView using Volley and NetworkImageView

2013-05-30   

Just added new example to the Android Volley Examples project which shows how to use Volley to populate dynamically ListView including loading of images using NetworkImageView. It also uses a simple read-ahead technique in order to load next page of data when the user reaches close to the end of the current page in order to minimize the wait.

dynamic-list-view-volley

Android Volley With GET and POST Parameters Example

2013-05-28   

Just added a simple example how to use GET and POST parameters to the Android Volley Examples project. You can find it under "GET and POST parameters" button.

Basically the example is quite simple: there is activity with two EditText fields used to enter a number and two buttons "Request with GET params" and "Request with POST params". You have to enter some digits in the fields, press one of the buttons and the request will be send to the server containing the parameters. Server will add the two numbers and return the sum which will be displayed bellow the buttons.

1. Request with GET parameters

In order to send request with GET parameters there are two alternatives:

1.1. To embed them directly in the string of the URL (as shown in the example) like:

String uri = String.format("http://ave.bolyartech.com/params.php?param1=%1$s¶m2=%2$s",
                           num1,
                           num2);
StringRequest myReq = new StringRequest(Method.GET,
                                        uri,
                                        createMyReqSuccessListener(),
                                        createMyReqErrorListener());
queue.add(myReq);

In the code above num1 and num2 hold the parameters' values.

1.2. If you are using Volley with with external HttpClient (4.2.x) you can use URIBuilder in order to build the URI in more convenient way

 

2. Request with POST parameters

You will need override getParams() method of the request and return a Map<String, String> that holds your parameters and their values like:

StringRequest myReq = new StringRequest(Method.POST,
                                        "http://ave.bolyartech.com/params.php",
                                        createMyReqSuccessListener(),
                                        createMyReqErrorListener()) {

    protected Map<string, string=""> getParams() throws com.android.volley.AuthFailureError {
        Map<string, string=""> params = new HashMap<string, string="">();
        params.put("param1", num1);
        params.put("param2", num2);
        return params;
    };
};
queue.add(myReq);

 

getpost

Using Android Volley With Self-Signed SSL Certificate

2013-05-28   

In brief:

  1. Get Volley from git clone https://android.googlesource.com/platform/frameworks/volley
  2. Get Android Volley Examples project from git clone git://github.com/ogrebgr/android_volley_examples.git
  3. Copy your keystore (BKS format) containing the self-signed public key in res/raw
  4. Open Act_SsSslHttpClient in the examples project, find "R.raw.test" and replace it with your keystore name (without the .pem extension)
  5. Find "new SslHttpClient(" and replace the default password "test123" with the password for your keystore
  6. Replace "44400" with the HTTPS port of your server/virtualhost. If you use the standart 443 -- then you may remove this parameter entirely
  7. Replace "https://tp.bolyartech.com:44400/https_test.html" with your  URL. Please make sure that you are using HTTPS otherwise it will work without as normal request, i.e. without encryption
  8. Start the app, go to "HTTPS with self-signed cert", then "Execute HTTPS request"
  9. If successful you will see something like "This is the result of successful HTTPS request. Congrats!". If some error occurres please check your logcat.
  10. Copy SslHttpClientSslSocketFactorySsX509TrustManager and your keystore to your project and enjoy! :-)

 

In details:

When you create an android app there is  no problem to execute HTTPS request against server with certificate issued by well-known Certification authority. However if you try to you use self-signed certificate you are in trouble -- certificate will be rejected by (more…)

Using Volley (Android) With External HttpClient (4.2.x)

2013-05-26   

By default Volley uses the built-in AndroidHttpClient on systems that are < Gingerbread and HttpUrlConnection on newer. For some this may not be enough. Someone may need Cookies support, other may need to work self-signed SSL certificate.   Probably there are few more reasons to use external HttpClient but most prominent one is that you may have old code that relies on it.

About 2 years ago I wrote Using newer version of HttpClient on Android (like 4.1.x). Now as follow up I added an example how to use Volley with such external HttpClient in my Android Volley Examples project.

Basically all I had to do is to create a new class ExtHttpClientStack, copy the functionality from the HttpClientStack and redirect all method calls from the built-in HttpClient to the external one.

Android Volley Examples

2013-05-20   

In brief: here is the git project with the Volley examples/samples

 

In details:

Volley was announced just few days ago at the I/O 2013. It is a library/framework that helps the developers to create more easily applications that use network requests, and more precisely REST requests. I was very exited to give it a try but unfortunately I found that there are no examples and tutorials. Now, after experimenting with it, I will try to fill to some extend that gap. (more…)

Downloading Android source as zip

2012-01-27   

I needed the source of Anroid Froyo in order to be able to debug it in Eclipse. On the official download page there  is a description how to download it under Linux (or cygwin) but at the time I did not had either so I tried to find plain, simple, good old zip that contains the entire source but it took me quite some time googling in order to find a site that provides such downloads. In order to save you from wasting time here it is:

http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/

If you need the source in order to debug it in Eclipse: download appropriate JAR (for example android-2.2_r1.1-sources.jar) , open it with zip and extract it in <android-sdk-dir>\platforms\<android-version>\sources (you may need to create "sources" subdir). Restart Eclipse and debug should work.

Using newer version of HttpClient on Android (like 4.1.x)

2011-08-20   

[EDIT] 2015-10-15: Now apache provides jar for android (currently 4.3.5.1). Also if you are targeting Android API 23 and newer you can use packages for Android maintained by Marek Sebera (currently 4.4.1.1).

[EDIT] This publication and method are old and deprecated. Please use http://code.google.com/p/httpclientandroidlib/ instead.

If you ended up here that is probably because you hit a bug  in the built-in HttpClient library which is not just old, it is beta quality (httpclient-4.0-beta1.jar). Strangely enough there is no hope that Android team will update it soon (also this is impossible without breaking backward compatibility…).

Fear not, there is a remedy.

Sadly we cannot replace the build-in library nor just add newer version but we can "trick" the system and move the library in another namespace in order to avoid conflict with the built-in lib.

The quick solution: here is an zip file containing all the required files (JARs converted using "ogrelab-" namespace prefix). Jump directly to step 7 in the "thorough" solution bellow.

The thorough solution:

Steps:

1. Download JarJar -- this is the tool which we will use to move the classes in new namespace

2. Download latest HttpClientCommonsLogging and Log4j (you will need "binary" packages) and extract them somewhere

3. Create a temporary dir somewhere like tmp_httpclient for example find following Jars and copy them into the temp dir (versions may differ):

  • commons-logging-1.1.1.jar
  • httpclient-4.1.2.jar
  • httpcore-4.1.2.jar
  • httpmime-4.1.2.jar
  • log4j-1.2.16.jar
Please note: there are additional JARs especially in the HttpClient archive but I don't use them, so I've excluded them from the list. Depending on your situation you may need them at some point so you will have to "convert" them too.
4. Copy jarjar-1.1.jar to the temp dir too (more…)

Activity lifecycle explained in details

2011-08-09   

Activity lifecycleUsually one of the biggest obstacles for the newcommers to android programming is the correct understanding of the activity's lifecycle, for example what is the difference in the event's chain when hitting "Back" button compared to activity being closed when phone is rotated. You cannot obtain this information by looking just at the activity lifecycle diagram in the API ref. In the following post I will use sample project that uses logging to demonstrate exactly what happens in the different cases. It is ment to be used as complementary tool for understanding how activities work.

Before proceeding you may want to download the sample project: Activity Lifecycle Demo. It contains simple logging of the activity and method name in each of the methods related to the lifecycle.

1. "Cold" start

Cold start is the case when the activity was a) started for the first time OR b) started after stopped with hitting "Back" button (the hardware one not <Button> that may exists in some activity's layout).

Logcat will show something like (timestamps will differ):

08-09 17:37:20.500: INFO/TEST(10107): A onCreate
08-09 17:37:20.507: INFO/TEST(10107): A onStart
08-09 17:37:20.507: INFO/TEST(10107): A onResume

Important to note here is that when onCreate(Bundle savedInstanceState) is called savedInstanceState is null. That is because there is no saved state because this is "cold" start (obviously).

2. Hitting "Back" button

08-09 17:50:19.050: INFO/TEST(10107): A onPause
08-09 17:50:19.433: INFO/TEST(10107): A onStop
08-09 17:50:19.433: INFO/TEST(10107): A onDestroy | isFinishing: true

Important to note here is isFinishing: true which means that call to isFinishing() in the onDestroy() returns true, i.e. which happens when: (more…)