org.oucho.whois.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.oucho.whois.MainActivity.java

Source

/**
 *  Whois for android, to get the registration information of a domain name.
 *
 * Copyright (C) 2016 Old Geek
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.oucho.whois;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private static final String fichier_prfrence="org.oucho.whois_preferences";
private static SharedPreferences prfrences=null;

    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        this.setTitle("");

        listenerOnSpinnerItemSelection();
        spinPosition();

        run();
    }

    private void listenerOnSpinnerItemSelection() {
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

private void spinPosition() {
    int position_pref = 0;

    prfrences = getSharedPreferences(fichier_prfrence, 0);

    int position = prfrences.getInt("tld_ID", position_pref);

    spinner.setSelection(position);
}

    private void run() {

        EditText urlText = (EditText) findViewById(R.id.editHostName);

        assert urlText != null;
        urlText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    whoisMessage(v);
                }
                return false;
            }
        });
    }

    @SuppressWarnings("UnusedParameters")
    public void clearHostNameMessage(View view) {
        EditText urlText = (EditText) findViewById(R.id.editHostName);
        assert urlText != null;
        urlText.setText("");

    }

@SuppressWarnings("UnusedParameters")
public void whoisMessage(View view) {

    String lookupString;
    String whoisInfoString;
    String selectedVal = "";

    View view2 = this.getCurrentFocus();
    if (view2 != null) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view2.getWindowToken(), 0);
    }

    prfrences = getSharedPreferences(fichier_prfrence, 0);
    String host = prfrences.getString("whois_value", selectedVal);

    final EditText urlText = (EditText) findViewById(R.id.editHostName);

    assert urlText != null;
    lookupString = urlText.getText().toString();

    LookupWhoisTask lookupWhoisTask = new LookupWhoisTask();
    whoisInfoString = runLookupTask(lookupWhoisTask, lookupString + ":" + host);

    TextView textView = (TextView) findViewById(R.id.whoisTextView);
    assert textView != null;
    textView.setTextSize(15);
    textView.setText(whoisInfoString);
}

    private String runLookupTask(LookupTask lookupTask, String lookupString) {
        AsyncTask<String, Void, String> task = lookupTask.execute(lookupString);
        String result = null;
        try {
            result = task.get();
            if (result == null) {

                String message = lookupTask.getExceptionMsg();
                String title = lookupTask.getException().toString();
                displayMessageDialog(message, title);
            }

        } catch (Exception taskE) {
            String message = taskE.getMessage();
            String title = taskE.toString();
            displayMessageDialog(message, title);
        }
        return result;
    }

    private void displayMessageDialog(String message, String title) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        alertDialogBuilder.setTitle(title);

        alertDialogBuilder.setMessage(message).setCancelable(false).setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();
    }

    private class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

    @SuppressLint("CommitPrefEdits")
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        prfrences = getSharedPreferences(fichier_prfrence, 0);
        SharedPreferences.Editor editor = prfrences.edit();

        String tld = parent.getItemAtPosition(position).toString();
        String selectedVal = getResources().getStringArray(R.array.whois_value)[parent.getSelectedItemPosition()];

        editor.putString("spinner_TLD", tld);
        editor.putInt("tld_ID", position);
        editor.putString("whois_value", selectedVal);
        editor.commit();
    }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // This constructor is intentionally empty, pourquoi ? parce que !
        }
    }

    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            System.exit(0);
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        Context context = getApplicationContext();

        getMenuInflater().inflate(R.menu.menu_main, menu);

        Drawable drawable = menu.findItem(R.id.about).getIcon();
        drawable.mutate();
        drawable.setColorFilter(ContextCompat.getColor(context, R.color.colorPrimaryDark),
                PorterDuff.Mode.SRC_ATOP);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about:
            about();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /**
     * About
     */
    private void about() {

        String title = getString(R.string.about);
        AlertDialog.Builder About = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        @SuppressLint("InflateParams")
        View dialoglayout = inflater.inflate(R.layout.alertdialog_main_noshadow, null);
        Toolbar toolbar = (Toolbar) dialoglayout.findViewById(R.id.dialog_toolbar_noshadow);
        toolbar.setTitle(title);
        toolbar.setTitleTextColor(0xffffffff);

        final TextView text = (TextView) dialoglayout.findViewById(R.id.showrules_dialog);
        text.setText(getString(R.string.about_message));

        About.setView(dialoglayout);

        AlertDialog dialog = About.create();
        dialog.show();

    }

}