com.swisscom.safeconnect.fragment.WhatsnewDialogFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.swisscom.safeconnect.fragment.WhatsnewDialogFragment.java

Source

/*
 Swisscom Safe Connect
 Copyright (C) 2014 Swisscom
    
 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 com.swisscom.safeconnect.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.swisscom.safeconnect.BuildConfig;
import com.swisscom.safeconnect.R;
import com.swisscom.safeconnect.utils.Config;
import com.swisscom.safeconnect.view.PipeButton;

import java.util.Locale;

public class WhatsnewDialogFragment extends DialogFragment {
    private WebView webView;
    private ProgressBar prBar;
    private Button btnOk;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getDialog() != null) {
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        }

        View v = inflater.inflate(R.layout.whatsnew, container, false);

        webView = (WebView) v.findViewById(R.id.view_whatsnew);
        prBar = (ProgressBar) v.findViewById(R.id.pb_loading);
        btnOk = (Button) v.findViewById(R.id.btn_ok);

        webView.setVerticalScrollBarEnabled(true);

        return v;
    }

    @Override
    public void onStart() {
        super.onStart();

        if (getDialog() == null)
            return;

        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // get version numbers
        String versionStr = "";
        try {
            PackageInfo packageInfo = getActivity().getPackageManager()
                    .getPackageInfo(getActivity().getPackageName(), 0);
            versionStr = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            if (BuildConfig.DEBUG)
                Log.e(Config.TAG, "error getting pkg info", e);
        }

        String[] versionArr = versionStr.split("\\.");

        int major = 0;
        int minor = 0;
        int revision = 0;

        if (versionArr.length >= 1) {
            major = parseInt(versionArr[0]);
        }

        if (versionArr.length >= 2) {
            minor = parseInt(versionArr[1]);
        }

        if (versionArr.length >= 3) {
            revision = parseInt(versionArr[2]);
        }

        webView.loadUrl(String.format("https://%s/whatsnew/and/?m=%d&i=%d&r=%d&l=%s", Config.PLUMBER_ADDR, major,
                minor, revision, Locale.getDefault().getLanguage()));
        prBar.setVisibility(View.VISIBLE);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeDlgOrActivity();
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (getDialog() != null) {
                    dismiss();
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                prBar.setVisibility(View.GONE);
            }
        });
    }

    private void closeDlgOrActivity() {
        if (getDialog() != null) {
            dismiss();
            return;
        }

        if (getActivity() != null) {
            getActivity().finish();
        }
    }

    private int parseInt(String str) {
        int res = 0;
        try {
            res = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            if (BuildConfig.DEBUG)
                Log.e(Config.TAG, "version is not an integer: " + str, e);
            res = 0;
        }
        return res;
    }
}