tk.android.client.activity.Activity_client.java Source code

Java tutorial

Introduction

Here is the source code for tk.android.client.activity.Activity_client.java

Source

package tk.android.client.activity;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import tk.android.client.R;
import tk.android.client.dialog.EditDialog;
import tk.android.client.dialog.LicenseDialog;
import tk.android.client.impl.OnFinishEditListener;
import tk.android.client.manager.PropertyManager;
import tk.android.client.manager.SQLiteManager;
import tk.android.client.twitter.Twitter_core;
import tk.android.client.util.UiHandler;
import twitter4j.Twitter;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class Activity_client extends FragmentActivity implements OnClickListener {
    private final String PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=tk.android.client";
    private String current_version = "";

    private FragmentActivity activity = this;
    private Twitter_core core;

    private Button button_version;
    private ProgressBar progressBar;
    private TextView textView_version;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);

        init();
    }

    private void init() {
        core = new Twitter_core(activity);
        button_version = (Button) activity.findViewById(R.id.button_version);
        progressBar = (ProgressBar) activity.findViewById(R.id.progressBar);
        textView_version = (TextView) activity.findViewById(R.id.textView_version);

        activity.findViewById(R.id.layout_support).setOnClickListener(this);
        activity.findViewById(R.id.layout_pro).setOnClickListener(this);
        activity.findViewById(R.id.layout_license).setOnClickListener(this);
        button_version.setOnClickListener(this);
        button_version.setClickable(false);

        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(),
                    PackageManager.PERMISSION_GRANTED);
            current_version = info.versionName;
            textView_version.setText("Version " + current_version);
        } catch (Exception e) {
            e.printStackTrace();
            textView_version.setText("Version Error");
        }

        new Thread(new Runnable() {

            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet get = new HttpGet(PLAY_STORE_URL);
                HttpResponse res = null;
                try {
                    res = httpClient.execute(get);
                } catch (IOException e) {
                    e.printStackTrace();
                    new UiHandler() {

                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                            button_version.setText(R.string.something_is_wrong);
                            button_version.setClickable(false);
                        }

                    }.post();
                    return;
                }

                try {
                    String source = EntityUtils.toString(res.getEntity());
                    Pattern p1 = Pattern
                            .compile("<div class=\"content\" itemprop=\"softwareVersion\"> [0-9.]+  </div>");
                    Matcher m1 = p1.matcher(source);
                    if (!m1.find()) {
                        Log.d("", "software version not found.");
                        return;
                    }
                    Pattern p2 = Pattern.compile("[0-9.]+");
                    Matcher m2 = p2.matcher(m1.group());
                    if (!m2.find()) {
                        Log.d("", "software version not found.");
                        return;
                    }
                    final String newest_version = m2.group();

                    new UiHandler() {

                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                            button_version.setText(newest_version.equals(current_version) ? R.string.newest_version
                                    : R.string.there_is_update);
                            button_version.setClickable(!newest_version.equals(current_version));
                        }

                    }.post();

                } catch (Exception e) {
                    e.printStackTrace();
                    new UiHandler() {

                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                            button_version.setText(R.string.something_is_wrong);
                            button_version.setClickable(false);
                        }

                    }.post();
                }
            }

        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_version: {
            try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(PLAY_STORE_URL)));
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
            }
            break;
        }
        case R.id.layout_support: {
            startActivity(new Intent(activity, Activity_user.class)
                    .setData(Uri.parse("https://twitter.com/OrangeSphereApp")));
            break;
        }
        case R.id.layout_pro: {
            if (PropertyManager.getInstance().isProUser()) {
                Toast.makeText(activity, "You alreadry Pro User :)", Toast.LENGTH_SHORT).show();
                break;
            }
            String TAG = "EditDialog-Pro";
            final EditDialog dialog = new EditDialog();
            dialog.newInstance(activity);
            dialog.setTitle(R.string.advertisement);
            dialog.setOnFinishEditListener(new OnFinishEditListener() {

                @Override
                public void onFinishEdit(final String text) {
                    int length = text.length();
                    if (length > 100 || length == 0) {
                        Toast.makeText(activity, R.string.bad_string_length, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    dialog.dismiss();
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                Twitter twitter = core.getTwitter();
                                if (twitter == null) {
                                    throw new Exception();
                                }
                                twitter.updateStatus(text + " " + PLAY_STORE_URL + " #OrangeSphere");
                                PropertyManager.getInstance().toBeProUser();
                                SQLiteManager manager = new SQLiteManager(activity);
                                SQLiteDatabase db = manager.getWritableDatabase();
                                ContentValues values = new ContentValues();
                                values.put("client_name", "OrangeSpherePro");
                                values.put("consumer_key", "b1NEqKgA8CGudo6yhkg");
                                values.put("consumer_secret", "awqnQwi3BXUqkyjlMZHlDIvad3bV3cVO03MxRTV4s");
                                db.insert("client_table", null, values);
                                db.close();
                                new UiHandler() {

                                    @Override
                                    public void run() {
                                        Toast.makeText(activity, R.string.be_pro_user, Toast.LENGTH_SHORT).show();
                                    }

                                }.post();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    }).start();
                }

            });
            dialog.show(getSupportFragmentManager(), TAG);
            break;
        }
        case R.id.layout_license: {
            new LicenseDialog().show(getSupportFragmentManager(), "LicenseDialog");
            break;
        }
        }
    }

}