Showing posts with label akkilis. Show all posts
Showing posts with label akkilis. Show all posts

Thursday, November 3, 2011

Simple cursor adapter made easy

Here is how to use the CursorAdapter in Android.

Outline:
In this example, we are gonna draw a list, using the CursorAdapter, for the cursor pointing to a table mapped with the list of my pojo "Promotion" basically containing the imageUrl, title and similar kind of info.
"ThumbnailFetchListener" and "downloadThumbnail" are interface and method respectively to download the images from server in the separate thread.
"promotion_banner" is the layout I use as the listViewItem.


 private class MyCursorAdapter extends CursorAdapter {


        LayoutInflater inflater;
        int titleColumnIndex;
        int imageUrlColumnIndex;

        public PromotionsGalleryAdapter(Context context, Cursor c,
                boolean autoRequery) {
            super(context, c, autoRequery);
            init(context, c);
        }
        private void init(Context context, Cursor c) {
            inflater = LayoutInflater.from(context);
            titleColumnIndex = c.getColumnIndexOrThrow(Promotions.TITLE);
            imageUrlColumnIndex = c.getColumnIndexOrThrow(Promotions.IMAGE_URL);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            Logger.log(TAG, "newView called");
            View v = inflater.inflate(R.layout.promotion_banner, null);
            v.setLayoutParams(new Gallery.LayoutParams(
                    FrameLayout.LayoutParams.FILL_PARENT,
                    FrameLayout.LayoutParams.FILL_PARENT));
            return v;
        }


        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            String title = cursor.getString(titleColumnIndex);
            String imageUrl = cursor.getString(imageUrlColumnIndex);
            Bitmap bitmap = null;
            if (TextUtils.isEmpty(imageUrl)) {
                view.findViewById(R.id.progress).setVisibility(View.GONE);
            } else {
                bitmap = BitmapCache.loadBitmap(imageUrl);
                if (bitmap == null) {
                    serviceHelper.downloadThumbnail(imageUrl,
                            new ThumbnailFetchListener());
                } else {
                    view.findViewById(R.id.progress).setVisibility(View.GONE);
                    view.findViewById(R.id.promotion_image_iv).setVisibility(
                            View.VISIBLE);
                }
            }
            ((ImageView) view.findViewById(R.id.promotion_image_iv))
                    .setImageBitmap(bitmap);
        }


        private class ThumbnailFetchListener implements
                ThumbnailDownloadListener {


            @Override
            public void onComplete(Bitmap bitmap, String url, String filename) {
                BitmapCache.save(url, bitmap);
                notifyDataSetChanged();
            }


            @Override
            public void onError(String url) {
                // ignore
            }        }    }

Saturday, April 16, 2011

Twitter integration in an Android App

Prerequisites:

  1. Consumer Key
  2. Consumer key secret
  3. Signpost-core and signpost-commonshttp libs
Approach:
launch the twitter authentication in a webview and present the login page to user in it.
When user enters the credentials, validate them and proceed.

Activity TwitterConnectWebActivity:
** You can copy the following whole activity in your code, as its a standalone code

public class TwitterConnectWebActivity extends Activity {
    private static final String TAG = "TwitterConnectWebActivity";
    private static String TWITTER_IMAGE_URL = "http://api.twitter.com/1/users/profile_image/@user.json";

    private static final int LOADING_DIALOG_ID = 0;
    protected final String CALLBACKURL = "OauthTwitter://taylorswiftapp";
    protected static CommonsHttpOAuthConsumer twitterHttpOauthConsumer;
    protected static OAuthProvider twitterHttpOauthprovider;
    protected int TWITTER_LOGIN_REQUEST_CODE = 1;
    protected int TWITTER_LOGOUT_REQUEST_CODE = 2;

    AccessToken twitterAuthToken;

    Context context;
    WebView webview;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tw_connect_web_activity);
        context = this;
        webview = (WebView) findViewById(R.id.webview);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        new RequestTokenSecretUrlTask().execute();
    }

    private class RequestTokenSecretUrlTask extends
            AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            showDialog(LOADING_DIALOG_ID);
        }

        @Override
        protected String doInBackground(Void... params) {
            try {
                twitterHttpOauthConsumer = new CommonsHttpOAuthConsumer( "YOUR_CONSUMER_KEY",
"YOUR_CONSUMER_SECRET");

                twitterHttpOauthprovider = new DefaultOAuthProvider(
                        "https://api.twitter.com/oauth/request_token",
                        "https://api.twitter.com/oauth/access_token",
                        "https://api.twitter.com/oauth/authorize");
                String authUrl = twitterHttpOauthprovider.retrieveRequestToken(
                        twitterHttpOauthConsumer, CALLBACKURL);
                Log.e(TAG, "Load URL : " + authUrl);
                return authUrl;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String authUrl) {
            dismissDialog(LOADING_DIALOG_ID);

            webview.setWebViewClient(new TwitterWebViewClient());
            webview.getSettings().setPluginsEnabled(true);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setLayoutAlgorithm(
                    WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            webview.loadUrl(authUrl);
        }
    }

    private class TwitterWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.e(TAG, "shouldOverrideUrlLoading() -> url:" + url);

            progressBar.setVisibility(View.VISIBLE);

            Uri uri = Uri.parse(url);
            if (uri != null && uri.toString().startsWith(CALLBACKURL)) {
                String verifier = uri
                        .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
                try {
                    twitterHttpOauthprovider.retrieveAccessToken(
                            twitterHttpOauthConsumer, verifier);

                    twitterAuthToken = new AccessToken(twitterHttpOauthConsumer
                            .getToken(), twitterHttpOauthConsumer
                            .getTokenSecret());

                    ConfigurationBuilder cb = new ConfigurationBuilder();
                    cb.setDebugEnabled(true).setOAuthConsumerKey(
                            twitterHttpOauthConsumer.getConsumerKey())
                            .setOAuthConsumerSecret(
                                    twitterHttpOauthConsumer
                                            .getConsumerSecret())
                            .setOAuthAccessToken(twitterAuthToken.getToken())
                            .setOAuthAccessTokenSecret(
                                    twitterAuthToken.getTokenSecret());
                    TwitterFactory tf = new TwitterFactory(cb.build());
                    Twitter twitter = tf.getInstance();

                    // if authentication is done,set result to ok finish this
                    // activity
                    Intent intent = new Intent();
                    intent
                            .putExtra("access_token", twitterAuthToken
                                    .getToken());
                    intent.putExtra("access_secret", twitterAuthToken
                            .getTokenSecret());

                    String name = twitter.getScreenName();

                    intent.putExtra("twitter_screen_name", name);
                    intent.putExtra("twitter_uid", "" + twitter.getId());
                    intent
                            .putExtra("twitter_image_url", ""
                                    + twitter.getProfileImage(name,
                                            ProfileImage.BIGGER));

                    setResult(RESULT_OK, intent);
                    finish();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            } else {
                Log.e(TAG, " ::2222:: Twitter URl --- " + url);
                view.loadUrl(url);
                return true;
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case LOADING_DIALOG_ID:
            ProgressDialog dialog = new ProgressDialog(this);
            dialog.setMessage("Loading...");
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface arg0) {
                    finish();
                }
            });
            return dialog;
        default:
            return super.onCreateDialog(id);
        }
    }
}


NOW, from the point where you wanna start this twitter stuff, just call:
startActivity(new Intent(this, TwitterConnectWebActivity.class));

In manifest add this permission:

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

You are done.