Wednesday, 2 December 2015

How to Integrate Youtube Video in Android Application

Before using the Android Youtube API, we need to register our application in the Google Developers Console. To register the application, follow these steps.

1. Go to the Google Developers Console
2. Create a new project.
3. From left slider, go to API/API Manager.
4. Click APIs. In the list of APIs, click on YouTube Data API and enable the Youtube Data API v3 on the page that appears.
5. In the left sidebar, select Credentials. Select API key from the Add Credentials dropdown menu. A popup will appear for you to specify the key type. Select Android Key. Next select Add package name and fingerprint and add the Android app's package name (add your package name here ie. com.neelam.videoyoutube) and then run the following command in Command prompt to get the SHA-1 certificate fingerprint.

keytool -list -v -keystore ~/.android/debug.keystore
Enter android as the password when prompted
Note: But before it you have to set path upto bin directory under java.



In the Terminal, we will see the MD5 and SHA-1 certificate fingerprints. Copy the SHA-1 fingerprint and paste it on the Google Developer Console and click on the Create button. An API key will be generated. We'll use this later.

Now come to the android project:

Create a class name Config.java and paste the following code there:

public class Config {
// Google Console APIs developer key
// Replace this key with your's
public static final String YOUTUBE_DEVELOPER_KEY = "AIzaSyCWAbCTOupVgfBAieR5NqAjUvIJ4lJ6FhA";

// YouTube video id
public static final String YOUTUBE_VIDEO_CODE = "fhWaJi1Hsfo";
}

Download the latest version of the YouTube Android Player API. Unzip the downloaded file to find the library jar file. Copy and paste it into your project's libs folder.

Modify activity_main.xml as shown.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>
Edit the strings.xml file as shown.

<resources>
    <string name="app_name">VideoYouTube</string>
    <string name="hello_world">Hello world!</string>    
    <string name="player_error">Error initializing YouTube player</string>    
</resources>
Modify MainActivity.java as shown.

package com.neelam.videoyoutube;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubeStandalonePlayer;

public class MainActivity extends YouTubeBaseActivity  {

private static final int RECOVERY_DIALOG_REQUEST = 1;

// YouTube player view
private YouTubePlayerView youTubeView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);

youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, Config.YOUTUBE_DEVELOPER_KEY , Config.YOUTUBE_VIDEO_CODE);
 startActivity(intent);
}
}

Add Internet permission in your AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Now our app is ready to test.
Happy Coding...


Tuesday, 7 October 2014

How to view your web-page source code in android device



First create a project with the name WebsiteAnalysisTools and have a main activity with following code:

package com.clearpath.websiteanalysistools;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

   EditText url_text;
   TextView textView;  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        url_text = (EditText) findViewById(R.id.address);
        textView = (TextView) findViewById(R.id.tv);
   }
    
    public void myButtonClickHandler(View view) {
        new GrabURL().execute();
    }
    
    private class GrabURL extends AsyncTask<String, Void, Void> {
        private final HttpClient Client = new DefaultHttpClient();
        private String response_str;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
        
        protected void onPreExecute() {
        Dialog.setMessage("Processing..");
            Dialog.show();
      }
  protected Void doInBackground(String... urls) {
            try {
             HttpGet request = new HttpGet(url_text.getText().toString());
                  // Get the response
                  ResponseHandler<String> responseHandler = new BasicResponseHandler();
                  response_str = Client.execute(request, responseHandler);
                  Log.d("HTML:", response_str);
            } catch (ClientProtocolException e) {
                Error = e.getMessage();
                cancel(true);
            } catch (IOException e) {
                Error = e.getMessage();
                cancel(true);
            }
            
            return null;
        }
        protected void onPostExecute(Void unused) {
            Dialog.dismiss();
           textView.setText(response_str);
            if (Error != null) {
                Toast.makeText(MainActivity.this, Error, Toast.LENGTH_LONG).show();
            } 
        }
        
    }

}


The main layout file is presented here which is activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <EditText android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/address"
        android:hint="http://www.google.com"
        android:singleLine="true">
    </EditText>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Read Webpage"
        android:id="@+id/ReadWebPage"
        android:onClick="myButtonClickHandler">
    </Button>

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tv"
            android:scrollbars="vertical">
        </TextView>
    </ScrollView>

</LinearLayout>

The manifest should have following permissions:

<uses-permission android:name="android.permission.INTERNET" />


Screenshot of the output:



Thursday, 25 September 2014

Decoding Android apk file (Reverse Engineering)

Procedure for decoding .apk files, step-by-step method:

Step 1:
Make a new folder and put .apk file in it (which you want to decode). Now rename the extension of this .apk file to .zip (eg.: rename from filename.apk to filename.apk.zip) and save it. Now you get classes.dex files, etc. At this stage you are able to see drawable but not xml and java files, so continue.

Step 2:
Now extract this zip apk file in the same folder (or NEW FOLDER). Now download dex2jar from this link
 http://code.google.com/p/dex2jar/  and extract it to the same folder (or NEW FOLDER). Now open command prompt and change directory to that folder (or NEW FOLDER). Then write dex2jar classes.dex and press enter. Now you get classes.dex.dex2jar file in the same folder. Then download java decompiler from  http://java.decompiler.free.fr/?q=jdgui  or from here and now double click on jd-gui  or AndroChef Java Decompiler 1.0 shortcut created on your desktop and click on open file. Then open classes.dex.dex2jar file from that folder. Now you get class files and save all these class files (click on file then click "save all sources" in jd-gui) by src name. At this stage you get java source but the xml files are still unreadable, so continue.

Step 3:
Now open another new folder and put these files
1.  put .apk file which you want to decode
2.  download apktool v1.x AND apktool install window (both can be downloaded at the same location) and put in the same folder
3.  download framework-res.apk file and put in the same folder (Not all apk file need framework-res.apk file)
4.  Open a command window
5.  Navigate to the root directory of APKtool and type the following command: apktool if framework-res.apk
6.  apktool d "fname".apk ("fname" denotes filename which you want to decode)
now you get a file folder in that folder and now you can easily read xml files also.

Monday, 24 June 2013

Signing Application Before Publishing on Play Store

To sign app we need to perform following 4 tasks.

  1.   Obtain a suitable private key
  2.  Compile the application in release mode
  3.  Sign your application with your private key
  4.  Align the final APK package
We perform these tasks by following given steps:
Step 1: Right click on the Android project. From the context menu, select, Android Tools -> Export Unsigned application package. Browse to the location you want your unsigned  apk  file to save and then click on SAVE.




Step 2:   Generating Certificate, keystore and signing app with private key.
If you have Keytool (installed in eclipse)  then all the three works can be done  in a single step.

To install the plugin in Eclipse Follow the following steps:
1.     Go to Option: HelpInstall New Software .
2.     Type “http://keytool.sourceforge.net/update in Work with:  text-area.
3.     Check the box Keytool and hit Finish button.
If Keytool checkbox does not appear then click add button and type http://keytool.sourceforge.net/update in Location Field of  Add Repository dialog box. In Name field type anything.

If Keytool is installed properly then your ecplipse will have such icon on tool bar:



 If you don't have a keystore(certificate for app) already, you will need to create one first. Once created, you should preserve this keystore somewhere, safely, so that you don't accidentally delete it. You will need this keystore everytime you want to sign and update your application to the Android Market. Please,  DO NOT LOSE THIS KEYSTORE.

When you’ll create it first time, a dialog box will pop-up as shown below. Just click “ok” and proceed.



Enter all the fields in this screen and press “Next”. Now your keystore will is created. Please remember all your passwords. Else, this keystore will be useless for you later.

NOTE: Alias name could be anything but better if you keep it as your app’s name for your convenience.


Browse to the location where your unsigned apk  file is saved. Enter Keystore password. For simplicity, keep both the passwords same. Click Finish. Your certificate is generated and your apk file is also signed after this 




                                        
Certificate will be generated like this:


Step 3:   Right click on the Android project. From the context menu, select, Android Tools -> Export Signed application package. In the dialog that will open, you will see the name of your project. If you have selected the wrong project, here is a chance to correct yourself. Click on "Next".




Step 4:  now perform Zipalign on your signed apk.
The zipalign tool is provided with the Android SDK, inside the tools/ directory. To align your signed APK, execute:
$ zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
The -v flag turns on verbose output (optional). 4 is the byte-alignment (don't use anything other than 4). The first file argument is your signed .apk file (the input) and the second file is the destination .apk file (the output). If you're overriding an existing APK, add the -f flag.
 You can download  Zipalign tool  and perform zipalign on your apk. Now your app is ready to upload apk on play store.
Caution: Your input APK must be signed with your private key before you optimize the package with zipalign. If you sign it after using zipalign, it will undo the alignment.