Android Open Source - UniTracker Main Fragment






From Project

Back to project page UniTracker.

License

The source code is released under:

MIT License

If you think the Android project UniTracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.connorroberts.unitracker.fragments;
//from  w  w  w. j ava 2 s.  c  o  m
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.connorroberts.unitracker.MainActivity;
import com.connorroberts.unitracker.R;
import com.connorroberts.unitracker.helpers.JSInterface;
import com.connorroberts.unitracker.helpers.SecurePreferences;

public class MainFragment extends Fragment {

    public WebView webView;

    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        SecurePreferences preferences = MainActivity.getSecurePreferences();

        final String personalID = preferences.getString("personalId");
        final String password = preferences.getString("password");

        webView = (WebView) rootView.findViewById(R.id.webView);
        webView.addJavascriptInterface(new JSInterface(getActivity().getApplicationContext(), rootView, getActivity()), "Android");

        WebViewClient webClient = new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                return false;

            }

            public void onPageFinished(WebView view, String url) {

                Log.e("", "Page Loaded - URL: " + url);

                if (url.equals("https://track.ucas.com/")) {

                    String loginJS = "javascript:(function () { " +
                                         "document.getElementById('PersonalId').value = '" + personalID + "';" +
                                         "document.getElementById('Password').value = '" + password + "';" +
                                         "document.getElementsByTagName('input')[3].click();" +
                                     "})()";

                    webView.loadUrl(loginJS);

                } else if (url.equals("https://track.ucas.com/Home")) {

                    String dataJS = "javascript:(function () { " +

                            "var offers = document.querySelectorAll('.offer-summary');" +

                            "for (var i = 0; i < offers.length; i++) {" +

                            "var choice = {" +
                                "'universityName' : offers[i].querySelectorAll('.offer-su .half .header')[0].firstChild.textContent.trim()," +
                                "'universityCode' : offers[i].querySelectorAll('.offer-su .half .header')[0].firstElementChild.innerText," +
                                "'courseCode' : offers[i].querySelectorAll('.offer-su .half .subheader .detail')[0].innerHTML," +
                                "'courseName' : offers[i].querySelectorAll('.offer-su .half .subheader')[0].firstChild.textContent.trim()," +
                                "'status' : offers[i].querySelectorAll('.offer-su .half .header')[1].innerHTML.trim()," +
                                "'startDate' : offers[i].querySelectorAll('.offer-su .half')[0].children[2].textContent.trim()" +
                            "};" +

                            "Android.passData(i + 1, JSON.stringify(choice));" +

                            "};" +

                            "})()";

                    webView.loadUrl(dataJS);


                }


            }

        };

        webView.setWebViewClient(webClient);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.loadUrl("https://track.ucas.com");

        return rootView;

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

}




Java Source Code List

com.connorroberts.unitracker.MainActivity.java
com.connorroberts.unitracker.fragments.LoginFragment.java
com.connorroberts.unitracker.fragments.MainFragment.java
com.connorroberts.unitracker.helpers.JSInterface.java
com.connorroberts.unitracker.helpers.SecurePreferences.java