Java tutorial
package hu.zelena.guide; import android.Manifest; import android.app.Fragment; import android.app.FragmentManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.widget.Toast; import com.google.android.gms.analytics.HitBuilders; import com.google.android.gms.analytics.Tracker; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import hu.zelena.guide.fragments.AboutFragment; import hu.zelena.guide.fragments.InternetFragment; import hu.zelena.guide.fragments.MagicbookFragment; import hu.zelena.guide.fragments.OthersFragment; import hu.zelena.guide.fragments.PhonesFragment; import hu.zelena.guide.fragments.PostPaidFragment; import hu.zelena.guide.fragments.PrePaidFragment; import hu.zelena.guide.fragments.TabletFragment; import hu.zelena.guide.fragments.WatchFragment; import hu.zelena.guide.modell.Schedule; import hu.zelena.guide.modell.Version; import hu.zelena.guide.util.ActivityHelper; import hu.zelena.guide.util.AnalyticsApplication; /** Copyright (C) <2017> <Patrik G. Zelena> 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/>. */ public class MainActivity extends AppCompatActivity { private static final int RESULT_SETTINGS = 1; final int MY_PERMISSIONS_REQUEST_READ_STORAGE = 0; String verName; /** * Back gomb gombnyomsra trtn NavBar zrs vagy kilps */ boolean doubleBackToExitPressedOnce = false; private String brand; /** * NavigationBar / Toolbar */ private DrawerLayout mDrawer; private Toolbar toolbar; private NavigationView nvDrawer; private ActionBarDrawerToggle drawerToggle; private boolean isDark = false; private boolean writeStorage = true; private boolean offlineMode = false; private boolean betaMode = false; private String analyticINFO; private boolean firstFrag = false; private Tracker mTracker; public static Intent getOpenFacebookIntent(Context context) { try { context.getPackageManager().getPackageInfo("com.facebook.katana", 0); return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://group/530837540309717")); } catch (Exception e) { return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/groups/530837540309717/")); } } @SuppressWarnings("ResourceType") @Override protected void onCreate(Bundle savedInstanceState) { AnalyticsApplication application = (AnalyticsApplication) getApplication(); mTracker = application.getDefaultTracker(); /* user permissions krelmezs futsidben */ ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }, MY_PERMISSIONS_REQUEST_READ_STORAGE); ActivityHelper.initialize(this); isDark = ActivityHelper.darkMode(this); if (isDark) { Log.d("DarkTheme: ", "TRUE"); setTheme(R.style.MainDarkTheme); } else { Log.d("DarkTheme: ", "False"); } SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (sharedPrefs.getBoolean("offline", false)) { offlineMode = true; } if (sharedPrefs.getBoolean("betaMode", false)) { betaMode = true; } super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckVersion(); // Toolbar az ActionBar helyre toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // DrawerLayout belltsa mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawerToggle = setupDrawerToggle(); // Toogle belltsa mDrawer.addDrawerListener(drawerToggle); // DrawerView belltsa nvDrawer = (NavigationView) findViewById(R.id.nvView); setupDrawerContent(nvDrawer); Fragment fragment = new AboutFragment(); if (fragment != null) { firstFrag = true; FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit(); } else { Intent i = new Intent(this, ErrorActivity.class); i.putExtra("error", "Nem sikerlt a HOME-FRAGMENT betltse. [Null object]"); startActivity(i); Log.e("MainActivity", "Error in creating fragment"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. switch (item.getItemId()) { case android.R.id.home: mDrawer.openDrawer(GravityCompat.START); return true; case R.id.menu_settings: Intent i = new Intent(this, UserSettingsActivity.class); writeStorage = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; i.putExtra("writeStorage", writeStorage); startActivityForResult(i, RESULT_SETTINGS); mTracker.send(new HitBuilders.EventBuilder().setCategory("Action").setAction("Settings").build()); break; } return super.onOptionsItemSelected(item); } private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { selectDrawerItem(menuItem); return true; } }); } public void selectDrawerItem(MenuItem menuItem) { Fragment fragment = null; Bundle bundle = new Bundle(); switch (menuItem.getItemId()) { case R.id.alcatel_frag: fragment = new PhonesFragment(); brand = "Alcatel"; analyticINFO = brand; break; case R.id.apple_frag: fragment = new PhonesFragment(); brand = "Apple"; analyticINFO = brand; break; case R.id.balckberry_frag: fragment = new PhonesFragment(); brand = "BlackBerry"; analyticINFO = brand; break; case R.id.cat_frag: fragment = new PhonesFragment(); brand = "CAT"; analyticINFO = brand; break; case R.id.honor_frag: fragment = new PhonesFragment(); brand = "Honor"; analyticINFO = brand; break; case R.id.HTC_frag: fragment = new PhonesFragment(); brand = "HTC"; analyticINFO = brand; break; case R.id.huawei_frag: fragment = new PhonesFragment(); brand = "Huawei"; analyticINFO = brand; break; case R.id.lenovo_frag: fragment = new PhonesFragment(); brand = "Lenovo"; analyticINFO = brand; break; case R.id.lg_frag: fragment = new PhonesFragment(); brand = "LG"; analyticINFO = brand; break; case R.id.samsung_frag: fragment = new PhonesFragment(); brand = "Samsung"; analyticINFO = brand; break; case R.id.sony_frag: fragment = new PhonesFragment(); brand = "Sony"; analyticINFO = brand; break; case R.id.watch_frag: fragment = new WatchFragment(); analyticINFO = "Watch"; break; case R.id.tablet_frag: fragment = new TabletFragment(); analyticINFO = "Tablet"; break; case R.id.postpaid_frag: fragment = new PostPaidFragment(); analyticINFO = "PostPaid"; break; case R.id.prepaid_frag: fragment = new PrePaidFragment(); analyticINFO = "PrePaid"; break; case R.id.net_frag: fragment = new InternetFragment(); analyticINFO = "Net"; break; case R.id.magicbook_frag: fragment = new MagicbookFragment(); analyticINFO = "MagicBook"; break; case R.id.others_frag: fragment = new OthersFragment(); analyticINFO = "Others"; break; default: break; } if (fragment != null) { mTracker.send(new HitBuilders.EventBuilder().setCategory("Action") .setAction(analyticINFO + " drawer select").build()); firstFrag = false; bundle.putString("brand", brand); fragment.setArguments(bundle); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit(); } else { Intent i = new Intent(this, ErrorActivity.class); i.putExtra("error", "Nem sikerlt a FRAGMENT betltse. [Null object]"); startActivity(i); Log.e("MainActivity", "Error in creating fragment"); } // Kivlasztott kijellse menuItem.setChecked(true); // Cm bellts setTitle(menuItem.getTitle()); // Drawer bezrsa mDrawer.closeDrawers(); } /** * ActionBarDrawerToggle szinkronizci */ @Override protected void onPostCreate(Bundle savedInstanceState) { // onRestoreInstanceState utni szinkronizci super.onPostCreate(savedInstanceState); drawerToggle.syncState(); } private ActionBarDrawerToggle setupDrawerToggle() { return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); } /** * Engedly krs lekezelse */ @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_STORAGE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.d("REQUEST:", "GRANTED"); } else { Log.d("REQUEST:", "DENIED"); sendSnack("Offline md letiltva."); } return; } } } /** * Publikus fggvnyek / Metdusok */ public void sendSnack(String msg) { View Snack = findViewById(R.id.snack_container); Snackbar snackbar = Snackbar.make(Snack, msg, Snackbar.LENGTH_LONG); snackbar.show(); } public void openFacebook(View view) { Intent i = getOpenFacebookIntent(getApplicationContext()); startActivity(i); } public boolean checkInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } public void sendFeedback(View view) { String version = BuildConfig.VERSION_NAME; Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("plain/text"); intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "zelena.patrik@simplesoft.hu" }); intent.putExtra(Intent.EXTRA_SUBJECT, "Mobiltuds Guide - Visszajelz"); intent.putExtra(Intent.EXTRA_TEXT, "Verzi: " + version); startActivity(Intent.createChooser(intent, "")); } public boolean getOfflineMode() { return offlineMode; } public void startTutorial(View view) { Intent intent = new Intent(MainActivity.this, TutorialActivity.class); startActivity(intent); } @Override public void onBackPressed() { if (mDrawer.isDrawerOpen(GravityCompat.START)) { mDrawer.closeDrawers(); } else { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } Fragment fragment = new AboutFragment(); if (fragment != null) { firstFrag = true; FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit(); setTitle("MOBILTUDS GUIDE"); final Menu menu = nvDrawer.getMenu(); for (int i = 0; i < menu.size(); i++) { MenuItem item = menu.getItem(i); if (item.hasSubMenu()) { SubMenu subMenu = item.getSubMenu(); for (int j = 0; j < subMenu.size(); j++) { MenuItem subMenuItem = subMenu.getItem(j); subMenuItem.setChecked(false); } } else { item.setChecked(false); } } } else { Intent i = new Intent(this, ErrorActivity.class); i.putExtra("error", "Nem sikerlt a HOME-FRAGMENT betltse. [Null object]"); startActivity(i); Log.e("MainActivity", "Error in creating fragment"); } this.doubleBackToExitPressedOnce = true; Toast.makeText(this, "Nyomja meg mgegyszer a kilpshez", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; } }, 2000); } } /** * Verzi ellenrzs (async) */ private void VersionNotify() { NotificationManager manager; Notification myNotication; manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(Intent.ACTION_VIEW); String url = "http://users.iit.uni-miskolc.hu/~zelena5/work/telekom/mobiltud/version/current/guide.apk"; if (betaMode) { url = "http://users.iit.uni-miskolc.hu/~zelena5/work/telekom/mobiltud/version/beta/guide.apk"; } intent.setData(Uri.parse(url)); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0); Notification.Builder builder = new Notification.Builder(MainActivity.this); builder.setAutoCancel(false); builder.setTicker("j verzi rhet el"); builder.setContentTitle("j verzi rhet el"); builder.setContentText("Letltshez rintsd meg"); builder.setSmallIcon(R.drawable.ic_stat_name); builder.setContentIntent(pendingIntent); builder.setSubText("Verzi vltozs"); //API level 16 builder.setNumber(100); builder.build(); builder.setAutoCancel(true); myNotication = builder.getNotification(); manager.notify(11, myNotication); } private void CheckVersion() { if (checkInternet()) { new HttpRequestVersion().execute(); } } /** * View clicekre iterakcik */ public void magicbookClick(View view) { Intent i = new Intent(this, WebViewActivity.class); i.putExtra("URL", "http://magicbook.telekom.intra/"); i.putExtra("title", "Magic Book"); startActivity(i); } public void phoneBookClick(View view) { Intent i = new Intent(this, WebViewActivity.class); i.putExtra("URL", "http://telefonkonyv.telekom.intra/applications/phonebook/"); i.putExtra("title", "Telefonknyv"); startActivity(i); } public void phoneListkClick(View view) { Intent i = new Intent(this, WebViewActivity.class); i.putExtra("URL", "https://docs.google.com/gview?embedded=true&url=http://www.telekom.hu/static-la/sw/file/akcios-keszulek-arlista-mobil-dijcsomagokhoz.pdf"); i.putExtra("title", "Kszlk rlista"); startActivity(i); } public void repairListClick(View view) { Intent intent = new Intent(Intent.ACTION_VIEW); String url = "http://magicbook.telekom.intra/mb/tmobile/tevekenysegek/jav_kesz_atv/gyartok_gyartoi_szervizek.pdf"; intent.setData(Uri.parse(url)); startActivity(intent); } public void stickCompClick(View view) { Intent intent = new Intent(Intent.ACTION_VIEW); String url = "http://magicbook.telekom.intra/mb/tmobile/tevekenysegek/jav_kesz_atv/Adateszkozok_operacios_rendszer_kompatibilitasa.pdf"; intent.setData(Uri.parse(url)); startActivity(intent); } public void accessoryListClick(View view) { Intent intent = new Intent(Intent.ACTION_VIEW); String url = "http://magicbook.telekom.intra/mb/tmobile/arlistak/tartozek_arlista.xls"; intent.setData(Uri.parse(url)); startActivity(intent); } public void rssRead(View view) { Intent i = new Intent(this, RssActivity.class); startActivity(i); } public void ussdCodeActivity(View view) { Intent i = new Intent(this, UssdCodeActivity.class); startActivity(i); } public void emailCollectIn(View view) { Intent intent = new Intent(Intent.ACTION_VIEW); String url = "http://ugyfelelmeny.telekom.intra/mobiltudik/Default.aspx"; intent.setData(Uri.parse(url)); startActivity(intent); } public void emailCollectOut(View view) { Intent i = new Intent(this, WebViewActivity.class); i.putExtra("URL", "http://www.telekom.hu/lakossagi/szolgaltatasok/mobiltudos"); i.putExtra("title", "Email gyjt - Public"); startActivity(i); } public void workSchedules(View view) { new HttpRequestSchedule().execute(); } /** * Tablet Click-ek */ public void tabSpec(View view) { String name = view.getTag().toString(); Intent i = new Intent(MainActivity.this, SpecsAvtivity.class); i.putExtra("brand", "tablet"); i.putExtra("phone", name); startActivity(i); } public void watchSpec(View view) { String name = view.getTag().toString(); Intent i = new Intent(MainActivity.this, WatchAvtivity.class); i.putExtra("watch", name); startActivity(i); } private class HttpRequestVersion extends AsyncTask<Void, Void, Version> { @Override protected Version doInBackground(Void... params) { try { String url = "http://users.iit.uni-miskolc.hu/~zelena5/work/telekom/mobiltud/version/current/ver"; if (betaMode) { url = "http://users.iit.uni-miskolc.hu/~zelena5/work/telekom/mobiltud/version/beta/ver"; } RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); Version ver = restTemplate.getForObject(url, Version.class); return ver; } catch (Exception e) { Log.e("VersionCheck", e.getMessage(), e); } return null; } @Override protected void onPostExecute(Version ver) { verName = ver.getVersion(); if (Integer.valueOf(verName) > Integer.valueOf(BuildConfig.VERSION_CODE)) { VersionNotify(); } } } private class HttpRequestSchedule extends AsyncTask<Void, Void, Schedule> { @Override protected Schedule doInBackground(Void... params) { try { final String url = "http://users.iit.uni-miskolc.hu/~zelena5/work/telekom/mobiltud/schedule/current"; RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); Schedule sch = restTemplate.getForObject(url, Schedule.class); return sch; } catch (Exception e) { Log.e("Main Activity", e.getMessage(), e); } return null; } @Override protected void onPostExecute(Schedule sch) { Intent i = new Intent(MainActivity.this, WebViewActivity.class); i.putExtra("URL", sch.getSchedule()); i.putExtra("title", "Beoszts"); startActivity(i); } } }