/* 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.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.swisscom.safeconnect.R; import com.swisscom.safeconnect.activity.IncidentsActivity; import com.swisscom.safeconnect.loader.IncidentsCntLoader; import com.swisscom.safeconnect.loader.StatisticsLoader; import com.swisscom.safeconnect.model.IncidentType; import com.swisscom.safeconnect.model.PlumberIncidentCountResponse; import com.swisscom.safeconnect.model.PlumberResponse; import com.swisscom.safeconnect.model.PlumberStatsResponse; import com.swisscom.safeconnect.utils.Config; /** * Created by vadim on 19.09.14. */ public class StatisticsFragment extends Fragment implements LoaderManager.LoaderCallbacks<PlumberResponse> { public final static int TRAFFIC_LOADER_ID = 1; public final static int INCCNT_LOADER_ID = 2; private TextView tvViruses; private TextView tvBlockedWebsites; private TextView tvDownloaded; private TextView tvUploaded; private TextView tvDownloadedInfo; private TextView tvUploadedInfo; private View viewMalware, viewBlockedUrl; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.dashboard_stats, container, false); tvViruses = (TextView) v.findViewById(R.id.tv_viruses); tvBlockedWebsites = (TextView) v.findViewById(R.id.tv_blocked_websites); tvDownloaded = (TextView) v.findViewById(R.id.tv_arrowdown); tvUploaded = (TextView) v.findViewById(R.id.tv_arrowup); tvDownloadedInfo = (TextView) v.findViewById(R.id.tv_down_info); tvUploadedInfo = (TextView) v.findViewById(R.id.tv_up_info); viewMalware = v.findViewById(R.id.rl_malware); viewBlockedUrl = v.findViewById(R.id.rl_blocked_urls); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); startLoader(); viewMalware.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), IncidentsActivity.class); intent.putExtra(IncidentsActivity.INCIDENT_TYPE, IncidentType.MALWARE); getActivity().startActivity(intent); } }); viewBlockedUrl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), IncidentsActivity.class); intent.putExtra(IncidentsActivity.INCIDENT_TYPE, IncidentType.BLOCKED_URL); getActivity().startActivity(intent); } }); } public void startLoader() { getLoaderManager().restartLoader(TRAFFIC_LOADER_ID, null, this); getLoaderManager().restartLoader(INCCNT_LOADER_ID, null, this); } public void stopLoader(){ getLoaderManager().destroyLoader(TRAFFIC_LOADER_ID); getLoaderManager().destroyLoader(INCCNT_LOADER_ID); } public void setDownload(int kbytes) { tvDownloaded.setText(kbToString(kbytes)); } public void setUpload(int kbytes) { tvUploaded.setText(kbToString(kbytes)); } public void setTrafficInfo(String text){ tvDownloadedInfo.setText(text); tvUploadedInfo.setText(text); } public void setIncidentsCnt(PlumberIncidentCountResponse inc) { tvBlockedWebsites.setText(getActivity().getResources(). getQuantityString(R.plurals.pl_blocked_urls, inc.getBlockedUrlCnt(), inc.getBlockedUrlCnt())); tvViruses.setText(getActivity().getResources(). getQuantityString(R.plurals.pl_viruses_found, inc.getMalwareCnt(), inc.getMalwareCnt())); } public static String kbToString(int kbytes) { if (kbytes < 1000) { return kbytes + " KB"; } else if (kbytes < 100000) { return String.format("%.2f MB", kbytes / 1000f); } else if (kbytes < 1000000) { return String.format("%d MB", kbytes / 1000); } else { return String.format("%.2f GB", kbytes / 1000000f); } } @Override public Loader<PlumberResponse> onCreateLoader(int id, Bundle args) { if (id == TRAFFIC_LOADER_ID) { return new StatisticsLoader(getActivity()); } else if (id == INCCNT_LOADER_ID) { return new IncidentsCntLoader(getActivity()); } else { throw new IllegalStateException("unknown loader id!!"); } } @Override public void onLoadFinished(Loader<PlumberResponse> loader, PlumberResponse data) { if (data != null) { if (data instanceof PlumberStatsResponse) { int recv; int sent; String infoText; switch (Config.getInstance().getStatPeriod()) { case SESSION: recv = ((PlumberStatsResponse)data).getCurrentStats().getKbytesSent(); sent = ((PlumberStatsResponse)data).getCurrentStats().getKbytesRecv(); infoText = getString(R.string.lab_stats_session); break; case TODAY: recv = ((PlumberStatsResponse)data).getTodayStats().getKbytesSent(); sent = ((PlumberStatsResponse)data).getTodayStats().getKbytesRecv(); infoText = getString(R.string.lab_stats_today); break; case MONTH: recv = ((PlumberStatsResponse)data).getMonthStats().getKbytesSent(); sent = ((PlumberStatsResponse)data).getMonthStats().getKbytesRecv(); infoText = getString(R.string.lab_stats_month); break; case OVERALL: recv = ((PlumberStatsResponse)data).getTotalStats().getKbytesSent(); sent = ((PlumberStatsResponse)data).getTotalStats().getKbytesRecv(); infoText = getString(R.string.lab_stats_total); break; default: recv = ((PlumberStatsResponse)data).getMonthStats().getKbytesSent(); sent = ((PlumberStatsResponse)data).getMonthStats().getKbytesRecv(); infoText = getString(R.string.lab_stats_month); break; } setDownload(recv); setUpload(sent); setTrafficInfo(infoText); } else if (data instanceof PlumberIncidentCountResponse) { setIncidentsCnt((PlumberIncidentCountResponse) data); } } } @Override public void onLoaderReset(Loader<PlumberResponse> loader) { //do nothing, we want to display the last loaded statistics } }