Java tutorial
package com.www.avtovokzal.org; import android.app.Activity; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.inputmethod.InputMethodManager; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.android.volley.DefaultRetryPolicy; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.VolleyLog; import com.android.volley.toolbox.StringRequest; import com.anjlab.android.iab.v3.BillingProcessor; import com.anjlab.android.iab.v3.TransactionDetails; import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.HitBuilders; import com.google.android.gms.analytics.Tracker; import com.mikepenz.google_material_typeface_library.GoogleMaterial; import com.mikepenz.iconics.IconicsDrawable; import com.mikepenz.materialdrawer.Drawer; import com.mikepenz.materialdrawer.DrawerBuilder; import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import com.www.avtovokzal.org.Adapter.AutocompleteCustomArrayAdapter; import com.www.avtovokzal.org.Adapter.RouteObjectResultAdapter; import com.www.avtovokzal.org.Adapter.StationObjectAdapter; import com.www.avtovokzal.org.Fragment.DatePickerFragment; import com.www.avtovokzal.org.Fragment.KoltsovoDialogFragment; import com.www.avtovokzal.org.Fragment.UpdateDialogFragment; import com.www.avtovokzal.org.Listener.CustomAutoCompleteTextChangedListener; import com.www.avtovokzal.org.Object.AutoCompleteObject; import com.www.avtovokzal.org.Object.RouteObjectResult; import com.www.avtovokzal.org.Object.StationsObject; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; public class MainActivity extends AppCompatSettingsActivity implements DatePickerDialog.OnDateSetListener, BillingProcessor.IBillingHandler { private static final int LAYOUT = R.layout.activity_main; private static final String LICENSE_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv5XXw+M1Yp9Nz7EbiKEBrknpsTRGV2NKZU8e6EMB3C0BvgiKvDiCQTqYJasfPj/ICsJ+oAfYMlJRS1y5V/fpOWYJCHr0vr7r+cgnd7GqKk5DMIxRe8hKMppqYDdTjW4oPuoS/qhH5mVapZWyOWh/kl4ZshAAmxnk9eRRA9W5zUz62jzAu30lwbr66YpwKulYYQw3wcOoBQcm9bYXMK4SEJKfkiZ7btYS1iDq1pshm9F5dW3E067JYdf4Sdxg9kLpVtOh9FqvHCrXai0stTf+0wLlBLOogNzPG9Gj7z2TVaZIdCWJKqZ97XP/Ur8kGBNaqDLCBSzm6IL+hsE5bzbmlQIDAQAB"; private static final String PRODUCT_ID = "com.www.avtovokzal.org.ads.disable"; public CustomAutoCompleteView myAutoComplete; public ArrayAdapter<AutoCompleteObject> myAdapter; public DatabaseHandler databaseH; private Button btnNextDay; private Drawer drawerResult = null; private ListView listView; private Menu toolbarMenu; private ProgressDialog queryDialog; private TextView textView; private FloatingActionButton fab; private BillingProcessor bp; private String code; private String dateNow; private String number; private String time; private String nameStation; private String versionGooglePlay = null; private int day = 0; private boolean cancel; private boolean load; private boolean sell; private boolean AdShowGone; private boolean update; private boolean all; private boolean showDialogKoltsovo; private static final String TAG = "MainActivity"; @Override @SuppressWarnings("ConstantConditions") protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(LAYOUT); boolean defaultStation; boolean checkMD5; boolean checkMD5ekb; boolean sendPhoneInfo; Button btnDate; databaseH = DatabaseHandler.getInstance(getApplicationContext()); // Google Analytics Tracker t = ((AppController) getApplication()).getTracker(AppController.TrackerName.APP_TRACKER); t.enableAdvertisingIdCollection(true); // ? ? ? settings = getSharedPreferences(Constants.APP_PREFERENCES, Context.MODE_PRIVATE); // ? AdShowGone = settings.contains(Constants.APP_PREFERENCES_ADS_SHOW) && settings.getBoolean(Constants.APP_PREFERENCES_ADS_SHOW, false); bp = new BillingProcessor(this, LICENSE_KEY, this); boolean isAvailable = BillingProcessor.isIabServiceAvailable(this); if (!isAvailable) { // Google Analytics t.send(new HitBuilders.EventBuilder().setCategory(getString(R.string.analytics_category_google)) .setAction(getString(R.string.analytics_action_google_result)).build()); } if (Constants.DEVELOPER) { AdShowGone = true; } // if (!AdShowGone) { initializeAd(R.id.adViewMainActivity); } loadSystemInfo(); // ? ? ? if (!databaseH.checkIfExistsRowTable("stations")) { SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_MD5_CHECK, false); editor.apply(); } if (!databaseH.checkIfExistsRowTable("stations_ekb")) { SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_MD5_EKB_CHECK, false); editor.apply(); } // ? ? ?? ? checkMD5 = settings.getBoolean(Constants.APP_PREFERENCES_MD5_CHECK, false); checkMD5ekb = settings.getBoolean(Constants.APP_PREFERENCES_MD5_EKB_CHECK, false); if (Constants.LOG_ON) Log.v("Check MD5 Ekb", " " + checkMD5ekb); if (!checkMD5) { if (Constants.LOG_ON) Log.v("checkMD5", "Load Station To DB"); loadStationToDB(); } if (!checkMD5ekb) { loadStationEkbToDB(); } // ? ? ? myAutoComplete = (CustomAutoCompleteView) findViewById(R.id.autoCompleteMain); listView = (ListView) findViewById(R.id.listViewMain); fab = (FloatingActionButton) findViewById(R.id.fab); // ? ?? ListView LayoutInflater inflater = getLayoutInflater(); ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.listview_footer, listView, false); listView.addFooterView(footer, null, false); // ? ? ? cancel = getSettingsParams(Constants.APP_PREFERENCES_CANCEL); sell = getSettingsParams(Constants.APP_PREFERENCES_SELL); load = getSettingsParams(Constants.APP_PREFERENCES_LOAD); all = getSettingsParams(Constants.APP_PREFERENCES_ALL); defaultStation = getSettingsParams(Constants.APP_PREFERENCES_DEFAULT); sendPhoneInfo = getSettingsParams(Constants.APP_PREFERENCES_SEND_PHONE_INFORMATION); showDialogKoltsovo = settings.getBoolean(Constants.APP_PREFERENCES_SHOW_DIALOG_KOLTSOVO, true); if (defaultStation && settings.contains(Constants.APP_PREFERENCES_STATION_CODE) && settings.contains(Constants.APP_PREFERENCES_STATION_NAME) && !load) { code = settings.getString(Constants.APP_PREFERENCES_STATION_CODE, null); nameStation = settings.getString(Constants.APP_PREFERENCES_STATION_NAME, null); myAutoComplete.setText(nameStation); loadScheduleResult(code, day, cancel, sell, all); } else if (!load && !defaultStation) { loadSchedule(day, cancel, sell); } listViewListener(); myAutoCompleteListener(); myAutoCompleteFocus(); myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(this)); AutoCompleteObject[] ObjectItemData = new AutoCompleteObject[0]; myAdapter = new AutocompleteCustomArrayAdapter(this, R.layout.listview_dropdown_item, ObjectItemData); // ? ? AllCaps btnDate = (Button) findViewById(R.id.header); btnNextDay = (Button) findViewById(R.id.buttonNextDay); btnDate.setTransformationMethod(null); btnNextDay.setTransformationMethod(null); initializeToolbar(R.string.app_name_city, R.string.app_subtitle_main); initializeNavigationDrawer(); initializeFloatingActionButton(); checkAdSettings(); if (!getSettingsParams(Constants.APP_PREFERENCES_CANCEL_CHECK_VERSION)) { getVersionFromGooglePlay(); } if (!sendPhoneInfo) { sendPhoneInformationToServer(); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_SEND_PHONE_INFORMATION, true); editor.apply(); } } private void sendPhoneInformationToServer() { String version = null; PackageInfo packageInfo = null; try { packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } String manufacturer = Uri.encode(Build.MANUFACTURER); String model = Uri.encode(Build.MODEL); String device = Uri.encode(Build.DEVICE); String board = Uri.encode(Build.BOARD); String brand = Uri.encode(Build.BRAND); String display = Uri.encode(Build.DISPLAY); String id = Uri.encode(Build.ID); String product = Uri.encode(Build.PRODUCT); String release = Uri.encode(Build.VERSION.RELEASE); if (packageInfo != null) { version = Uri.encode(packageInfo.versionName); } if (Constants.LOG_ON) { Log.v(TAG, "1: " + manufacturer + " 2: " + model + " 3: " + device + " 4: " + board + " 5: " + brand + " 6: " + display + " 7: " + id + " 8: " + product + " 9: " + release + " 10: " + version); } String url = "http://www.avtovokzal.org/php/app/sendPhoneInformation.php?manufacturer=" + manufacturer + "&model=" + model + "&device=" + device + "&board=" + board + "&brand=" + brand + "&display=" + display + "&build_id=" + id + "&product=" + product + "&release_number=" + release + "&version=" + version; if (isOnline()) { StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (Constants.LOG_ON) VolleyLog.d(TAG, "Error: " + error.getMessage()); } }); // ? TimeOut, Retry strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // ? ? AppController.getInstance().addToRequestQueue(strReq); } else { callErrorActivity(); } } private void myAutoCompleteFocus() { // ? AutoComplete ? ? myAutoComplete.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean bool) { if (bool) { myAutoComplete.setText(""); } } }); } private void myAutoCompleteListener() { myAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View agr1, int pos, long id) { RelativeLayout rl = (RelativeLayout) agr1; TextView tv = (TextView) rl.getChildAt(0); nameStation = tv.getText().toString(); myAutoComplete.setText(nameStation); // ? InputMethodManager inputMethodManager = (InputMethodManager) getSystemService( Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(myAutoComplete.getWindowToken(), 0); code = tv.getTag().toString(); // ? ? loadScheduleResult(code, day, cancel, sell, all); myAutoComplete.clearFocus(); if (Integer.parseInt(code) == 102 && showDialogKoltsovo) { FragmentManager manager = getSupportFragmentManager(); KoltsovoDialogFragment dialogFragment = new KoltsovoDialogFragment(); dialogFragment.show(manager, "dialog"); } } }); myAutoComplete.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) { int count = myAdapter.getCount(); if (count > 0) { AutoCompleteObject object = myAdapter.getItem(0); myAutoComplete.setText(object.toString()); code = object.getObjectCode(); // ? InputMethodManager inputMethodManager = (InputMethodManager) getSystemService( Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(myAutoComplete.getWindowToken(), 0); // ? ? loadScheduleResult(code, day, cancel, sell, all); myAutoComplete.clearFocus(); if (Integer.parseInt(code) == 102 && showDialogKoltsovo) { FragmentManager manager = getSupportFragmentManager(); KoltsovoDialogFragment dialogFragment = new KoltsovoDialogFragment(); dialogFragment.show(manager, "dialog"); } } } return false; } }); } private void listViewListener() { listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String name; String numberToView; RelativeLayout rl = (RelativeLayout) view; TextView textViewTime = (TextView) rl.getChildAt(0); TextView textViewNumber = (TextView) rl.getChildAt(1); TextView textViewName = (TextView) rl.getChildAt(2); time = textViewTime.getText().toString(); number = textViewNumber.getTag().toString(); numberToView = textViewNumber.getText().toString(); name = textViewName.getText().toString(); Intent intent = new Intent(getApplicationContext(), InfoActivity.class); intent.putExtra("number", number); intent.putExtra("numberToView", numberToView); intent.putExtra("time", time); intent.putExtra("name", name); intent.putExtra("day", day); startActivity(intent); } }); listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int def = totalItemCount - (firstVisibleItem + visibleItemCount); if (totalItemCount > 0 && def < 3 && fab.getVisibility() == View.VISIBLE) { Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out); fab.setAnimation(fadeOutAnimation); fab.setVisibility(View.GONE); } else if (totalItemCount > 0 && def > 3 && fab.getVisibility() != View.VISIBLE) { Animation fadeInAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in); fab.setAnimation(fadeInAnimation); fab.setVisibility(View.VISIBLE); } } }); } private void initializeFloatingActionButton() { fab.setImageDrawable( new IconicsDrawable(this).icon(GoogleMaterial.Icon.gmd_vertical_align_bottom).color(Color.WHITE)); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Google Analytics Tracker t = ((AppController) getApplication()).getTracker(AppController.TrackerName.APP_TRACKER); t.send(new HitBuilders.EventBuilder().setCategory(getString(R.string.analytics_category_button)) .setAction(getString(R.string.analytics_action_fab)).build()); Intent intentArrival = new Intent(MainActivity.this, ArrivalActivity.class); if (code != null) { intentArrival.putExtra("code", code); intentArrival.putExtra("newNameStation", nameStation); } startActivity(intentArrival); } }); } private void initializeNavigationDrawer() { drawerResult = new DrawerBuilder().withActivity(this).withToolbar(toolbar).withDisplayBelowStatusBar(true) .withActionBarDrawerToggleAnimated(true).addDrawerItems(getDrawerItems()) .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override public boolean onItemClick(View view, int position, IDrawerItem iDrawerItem) { switch (position) { case 1: drawerResult.closeDrawer(); return true; case 2: drawerResult.closeDrawer(); Intent intentArrival = new Intent(MainActivity.this, ArrivalActivity.class); if (code != null) { intentArrival.putExtra("code", code); intentArrival.putExtra("newNameStation", nameStation); } startActivity(intentArrival); overridePendingTransition(R.animator.slide_in_right, R.animator.slide_out_left); return true; case 4: drawerResult.closeDrawer(); Intent intentEtraffic = new Intent(MainActivity.this, EtrafficActivity.class); startActivity(intentEtraffic); overridePendingTransition(R.animator.slide_in_right, R.animator.slide_out_left); return true; case 6: drawerResult.closeDrawer(); Intent intentEtrafficMain = new Intent(MainActivity.this, EtrafficMainActivity.class); startActivity(intentEtrafficMain); overridePendingTransition(R.animator.slide_in_right, R.animator.slide_out_left); return true; case 8: drawerResult.closeDrawer(); Intent intentMenu = new Intent(MainActivity.this, MenuActivity.class); intentMenu.putExtra("day", day); intentMenu.putExtra("activity", "MainActivity"); if (code != null) { intentMenu.putExtra("code", code); } startActivity(intentMenu); overridePendingTransition(R.animator.slide_in_right, R.animator.slide_out_left); return true; case 9: drawerResult.closeDrawer(); Intent intentAbout = new Intent(MainActivity.this, AboutActivity.class); startActivity(intentAbout); overridePendingTransition(R.animator.slide_in_right, R.animator.slide_out_left); return true; } return false; } }).build(); drawerResult.setSelection(1); } private void checkAdSettings() { if (settings.contains(Constants.APP_PREFERENCES_AD_DATE)) { String dateSettings = settings.getString(Constants.APP_PREFERENCES_AD_DATE, null); if (dateSettings != null) { if (dateSettings.length() > 3) { SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", java.util.Locale.getDefault()); Calendar calendar = Calendar.getInstance(); Calendar calendarSettings = Calendar.getInstance(); try { calendarSettings.setTime(sdf.parse(dateSettings)); } catch (ParseException e) { e.printStackTrace(); } long diff = calendar.getTimeInMillis() - calendarSettings.getTimeInMillis(); int countDays = (int) (diff / (24 * 60 * 60 * 1000)); if (Constants.LOG_ON) Log.v(TAG, String.valueOf(countDays)); if (countDays > 6) { SharedPreferences.Editor editor = settings.edit(); editor.putString(Constants.APP_PREFERENCES_AD_DATE, null); editor.putBoolean(Constants.APP_PREFERENCES_ADS_SHOW, false); editor.apply(); if (adView == null) { initializeAd(R.id.adViewMainActivity); } } } } } } @Override public void onBackPressed() { String version = null; try { PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0); version = info.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if (drawerResult != null && drawerResult.isDrawerOpen()) { drawerResult.closeDrawer(); } else if (version != null && versionGooglePlay != null && !version.equals(versionGooglePlay) && !getSettingsParams(Constants.APP_PREFERENCES_CANCEL_CHECK_VERSION)) { FragmentManager manager = getSupportFragmentManager(); UpdateDialogFragment dialogFragment = new UpdateDialogFragment(); dialogFragment.show(manager, "dialog"); } else { super.onBackPressed(); } } @Override protected void onStart() { super.onStart(); GoogleAnalytics.getInstance(this).reportActivityStart(this); } @Override protected void onStop() { super.onStop(); GoogleAnalytics.getInstance(this).reportActivityStop(this); } @Override protected void onPause() { if (adView != null) { adView.pause(); } super.onPause(); } @Override protected void onResume() { super.onResume(); if (adView != null) { adView.resume(); } if (adView != null && getSettingsParams(Constants.APP_PREFERENCES_ADS_SHOW)) { adView.setVisibility(View.GONE); } drawerResult.setSelection(1); } @Override protected void onDestroy() { if (adView != null) { adView.destroy(); } if (bp != null) { bp.release(); } queryDialogDismiss(); super.onDestroy(); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); String newNameStation = getIntent().getStringExtra("newNameStation"); code = getIntent().getStringExtra("code"); cancel = getIntent().getBooleanExtra("cancel", getSettingsParams(Constants.APP_PREFERENCES_CANCEL)); sell = getIntent().getBooleanExtra("sell", getSettingsParams(Constants.APP_PREFERENCES_SELL)); day = getIntent().getIntExtra("day", 0); load = getIntent().getBooleanExtra("cancel_load", getSettingsParams(Constants.APP_PREFERENCES_LOAD)); all = getIntent().getBooleanExtra("all", getSettingsParams(Constants.APP_PREFERENCES_ALL)); if (code != null) { loadScheduleResult(code, day, cancel, sell, all); if (newNameStation != null) { myAutoComplete.setText(newNameStation); } } else if (!load) { loadSchedule(day, cancel, sell); } } @Override public boolean onCreateOptionsMenu(Menu menu) { toolbarMenu = menu; getMenuInflater().inflate(R.menu.menu_items, menu); MenuItem item = toolbarMenu.findItem(R.id.lamp); item.setVisible(false); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.lamp: Toast.makeText(getApplicationContext(), getString(R.string.main_status), Toast.LENGTH_LONG).show(); return true; } return super.onOptionsItemSelected(item); } // ? private void loadStationToDB() { String url = "http://www.avtovokzal.org/php/app/station.php"; if (isOnline()) { StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { if (response == null) { callErrorActivity(); finish(); } // ? ? myAutoComplete.setEnabled(false); btnNextDay.setEnabled(false); processingLoadStationToDB task = new processingLoadStationToDB(); task.execute(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (Constants.LOG_ON) VolleyLog.d(TAG, "Error: " + error.getMessage()); callErrorActivity(); } }); // ? TimeOut, Retry strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // ? ? AppController.getInstance().addToRequestQueue(strReq); } else { callErrorActivity(); } } private void loadStationEkbToDB() { String url = "http://www.avtovokzal.org/php/app/station_ekb.php"; if (isOnline()) { StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { if (response == null) { callErrorActivity(); finish(); } processingLoadStationEkbToDB task = new processingLoadStationEkbToDB(); task.execute(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (Constants.LOG_ON) VolleyLog.d(TAG, "Error: " + error.getMessage()); callErrorActivity(); } }); // ? TimeOut, Retry strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // ? ? AppController.getInstance().addToRequestQueue(strReq); } else { callErrorActivity(); } } // ??? ? private void loadSchedule(Object... params) { String url = "http://www.avtovokzal.org/php/app/main.php?day=" + params[0] + "&cancel=" + params[1] + "&sell=" + params[2]; if (isOnline()) { queryDialog = new ProgressDialog(MainActivity.this); queryDialog.setMessage(getString(R.string.main_load)); queryDialog.setCancelable(false); queryDialog.show(); StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { if (response == null) { callErrorActivity(); finish(); } processingLoadSchedule task = new processingLoadSchedule(); task.execute(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (Constants.LOG_ON) VolleyLog.d(TAG, "Error: " + error.getMessage()); queryDialogDismiss(); callErrorActivity(); } }); // ? TimeOut, Retry strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // ? ? AppController.getInstance().addToRequestQueue(strReq); } else { callErrorActivity(); } } // ? ??? private void loadScheduleResult(Object... params) { String url = "http://www.avtovokzal.org/php/app/result_1.3.8.php?id=" + params[0] + "&day=" + params[1] + "&cancel=" + params[2] + "&sell=" + params[3] + "&all=" + params[4]; if (isOnline()) { queryDialog = new ProgressDialog(MainActivity.this); queryDialog.setMessage(getString(R.string.main_load)); queryDialog.setCancelable(false); queryDialog.show(); StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { if (response == null) { callErrorActivity(); finish(); } processingLoadScheduleResult task = new processingLoadScheduleResult(); task.execute(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (Constants.LOG_ON) VolleyLog.d(TAG, "Error: " + error.getMessage()); queryDialogDismiss(); callErrorActivity(); } }); // ? TimeOut, Retry strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // ? ? AppController.getInstance().addToRequestQueue(strReq); } else { callErrorActivity(); } } // ? , ?, ? private void loadSystemInfo() { String url = "http://www.avtovokzal.org/php/app/system_1.3.5_v1.php"; if (isOnline()) { StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { String md5hashFromSettings; String md5hashEkbFromSettings; if (response == null) { callErrorActivity(); finish(); } JSONObject dataJsonQbj; if (Constants.LOG_ON) Log.v("Result", response); try { dataJsonQbj = new JSONObject(response); JSONArray system = dataJsonQbj.getJSONArray("system"); JSONObject oneObject = system.getJSONObject(0); dateNow = oneObject.getString("date"); update = oneObject.getBoolean("update"); String md5hash = oneObject.getString("md5"); String md5hashEkb = oneObject.getString("md5_ekb"); // ?? ? if (!update && toolbarMenu != null) { MenuItem item = toolbarMenu.findItem(R.id.lamp); item.setVisible(true); } // ? textView = (TextView) findViewById(R.id.header); String string = getString(R.string.main_schedule) + " " + dateNow; textView.setText(string); SharedPreferences.Editor editorDate = settings.edit(); editorDate.putString(Constants.APP_PREFERENCES_DATE, dateNow); editorDate.apply(); // ?? ? ?? ? md5hashFromSettings = settings.getString(Constants.APP_PREFERENCES_MD5, ""); md5hashEkbFromSettings = settings.getString(Constants.APP_PREFERENCES_MD5_EKB, ""); if (Constants.LOG_ON) { Log.v("MD5 from Settings Ekb", md5hashEkbFromSettings + " " + md5hashEkb.equals(md5hashEkbFromSettings)); Log.v("MD5 Ekb", md5hashEkb); Log.v("MD5 from Settings", md5hashFromSettings + " " + md5hash.equals(md5hashFromSettings)); Log.v("MD5", md5hash); } // ? md5 ? ? ? if (!md5hash.equals(md5hashFromSettings)) { SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_MD5_CHECK, false); editor.putString(Constants.APP_PREFERENCES_MD5, md5hash); editor.apply(); if (Constants.LOG_ON) Log.v("Settings", "Station false"); } if (!md5hashEkb.equals(md5hashEkbFromSettings)) { SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_MD5_EKB_CHECK, false); editor.putString(Constants.APP_PREFERENCES_MD5_EKB, md5hashEkb); editor.apply(); if (Constants.LOG_ON) Log.v("Settings", "Station_ekb false"); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (Constants.LOG_ON) VolleyLog.d(TAG, "Error: " + error.getMessage()); callErrorActivity(); } }); // ? TimeOut, Retry strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // ? ? AppController.getInstance().addToRequestQueue(strReq); } else { callErrorActivity(); } } // ?? ? public void onClickNextDay(View view) { // Google Analytics Tracker t = ((AppController) getApplication()).getTracker(AppController.TrackerName.APP_TRACKER); t.send(new HitBuilders.EventBuilder().setCategory(getString(R.string.analytics_category_button)) .setAction(getString(R.string.analytics_action_next_day)).build()); String dateNew; day = day + 1; if (day >= 0 && day <= 9) { dateNew = dateNow; SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", java.util.Locale.getDefault()); Calendar c = Calendar.getInstance(); try { c.setTime(sdf.parse(dateNew)); } catch (ParseException e) { e.printStackTrace(); } c.add(Calendar.DATE, day); dateNew = sdf.format(c.getTime()); String string = getString(R.string.main_schedule) + " " + dateNew; textView.setText(string); if (code != null) { loadScheduleResult(code, day, cancel, sell, all); } else { loadSchedule(day, cancel, sell); } } else { Toast.makeText(getApplicationContext(), getString(R.string.main_invalid_date), Toast.LENGTH_SHORT) .show(); } } @Override public void onDateSet(DatePicker view, int year, int month, int dayDatePicker) { if (dateNow != null) { String monthNumber; String dayNumber; SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", java.util.Locale.getDefault()); Calendar calendarNow = Calendar.getInstance(); try { calendarNow.setTime(sdf.parse(dateNow)); } catch (ParseException e) { e.printStackTrace(); } Calendar calendarNew = Calendar.getInstance(); try { calendarNew.setTime(sdf.parse(dayDatePicker + "." + (month + 1) + "." + year)); } catch (ParseException e) { e.printStackTrace(); } long diff = calendarNew.getTimeInMillis() - calendarNow.getTimeInMillis(); int days = (int) (diff / (24 * 60 * 60 * 1000)); day = 0; day = day + days; if (month < 9) { monthNumber = "0" + (month + 1); } else { monthNumber = "" + (month + 1); } if (dayDatePicker < 10) { dayNumber = "0" + dayDatePicker; } else { dayNumber = "" + dayDatePicker; } if (code != null && days >= 0 && days <= 9) { loadScheduleResult(code, day, cancel, sell, all); String string = getString(R.string.main_schedule) + " " + dayNumber + "." + monthNumber + "." + year; textView.setText(string); // fix for Android 4.4.4 queryDialogDismiss(); } else if (days >= 0 && days <= 9) { loadSchedule(day, cancel, sell); String string = getString(R.string.main_schedule) + " " + dayNumber + "." + monthNumber + "." + year; textView.setText(string); // fix for Android 4.4.4 queryDialogDismiss(); } else { Toast.makeText(getApplicationContext(), getString(R.string.main_invalid_date), Toast.LENGTH_SHORT) .show(); } } } // public void showDatePickerDialog(View v) { // Google Analytics Tracker t = ((AppController) getApplication()).getTracker(AppController.TrackerName.APP_TRACKER); t.send(new HitBuilders.EventBuilder().setCategory(getString(R.string.analytics_category_button)) .setAction(getString(R.string.analytics_action_set_date)).build()); DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); } // Error Activity private void callErrorActivity() { Intent intent = new Intent(getApplicationContext(), ErrorActivity.class); intent.putExtra("activity", TAG); intent.putExtra("code", code); intent.putExtra("day", day); intent.putExtra("cancel", cancel); intent.putExtra("sell", sell); startActivity(intent); finish(); } private void getVersionFromGooglePlay() { String url = "http://www.avtovokzal.org/php/app/requestVersion.php"; StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { if (response != null) { try { JSONObject dataJsonObject = new JSONObject(response); versionGooglePlay = dataJsonObject.getString("version_app"); } catch (JSONException e) { e.printStackTrace(); } } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); // ? TimeOut, Retry strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // ? ? AppController.getInstance().addToRequestQueue(strReq); } @Override public void onProductPurchased(String productId, TransactionDetails details) { } @Override public void onPurchaseHistoryRestored() { } @Override public void onBillingError(int errorCode, Throwable error) { } @Override public void onBillingInitialized() { bp.loadOwnedPurchasesFromGoogle(); if (bp.isPurchased(PRODUCT_ID)) { // ? ? SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_ADS_SHOW, true); editor.putString(Constants.APP_PREFERENCES_AD_DATE, null); editor.apply(); if (adView != null) { adView.setVisibility(View.GONE); } AdShowGone = true; } } private class processingLoadSchedule extends AsyncTask<String, Void, List<StationsObject>> { @Override protected List<StationsObject> doInBackground(String... response) { JSONObject dataJsonQbj; List<StationsObject> list = new ArrayList<>(); if (Constants.LOG_ON) Log.v("Result", response[0]); try { dataJsonQbj = new JSONObject(response[0]); JSONArray rasp = dataJsonQbj.getJSONArray("rasp"); if (Constants.LOG_ON) Log.v("Length", " " + rasp.length()); if (rasp.length() == 0) { runOnUiThread(new Runnable() { @Override @SuppressWarnings("ConstantConditions") public void run() { TextView textView1 = (TextView) findViewById(R.id.noItems); textView1.setVisibility(View.VISIBLE); } }); } else { runOnUiThread(new Runnable() { @Override @SuppressWarnings("ConstantConditions") public void run() { TextView textView1 = (TextView) findViewById(R.id.noItems); textView1.setVisibility(View.GONE); } }); } for (int i = 0; i < rasp.length(); i++) { JSONObject oneObject = rasp.getJSONObject(i); String timeMarsh = oneObject.getString("time_otpr"); String numberMarsh = oneObject.getString("new_number_data"); String nameMarsh = oneObject.getString("name_data"); String nameBus = oneObject.getString("name_bus"); String countBus = oneObject.getString("count_bus"); String freeBus = oneObject.getString("free_place_new_schedule"); int cancelBus = oneObject.getInt("cancel_new_schedule"); String numberMarshToSend = oneObject.getString("number_new_schedule"); list.add(new StationsObject(timeMarsh, numberMarsh, numberMarshToSend, nameMarsh, nameBus, countBus, freeBus, cancelBus)); } } catch (JSONException e) { e.printStackTrace(); } return list; } @Override protected void onPostExecute(List<StationsObject> list) { final StationObjectAdapter adapter = new StationObjectAdapter(MainActivity.this, list); listView.setAdapter(adapter); super.onPostExecute(list); queryDialogDismiss(); } } private class processingLoadStationToDB extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... response) { JSONObject dataJsonQbj; String tableName = "stations"; if (Constants.LOG_ON) Log.v(TAG, response[0]); try { dataJsonQbj = new JSONObject(response[0]); JSONArray rasp = dataJsonQbj.getJSONArray("station"); databaseH.removeAll(tableName); for (int i = 0; i < rasp.length(); i++) { JSONObject oneObject = rasp.getJSONObject(i); String nameStation = oneObject.getString("name_station"); String noteStation = oneObject.getString("note_station"); long codeStation = oneObject.getLong("id_station"); long sumStation = oneObject.getLong("sum_station"); databaseH.create( new AutoCompleteObject((nameStation + " " + noteStation), sumStation, codeStation), tableName); } SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_MD5_CHECK, true); editor.apply(); if (Constants.LOG_ON) Log.v("Settings", "Station true"); } catch (JSONException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); // ? ? ? myAutoComplete.setEnabled(true); btnNextDay.setEnabled(true); } } private class processingLoadStationEkbToDB extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... response) { JSONObject dataJsonQbj; String tableName = "stations_ekb"; if (Constants.LOG_ON) Log.v(TAG, response[0]); try { dataJsonQbj = new JSONObject(response[0]); JSONArray rasp = dataJsonQbj.getJSONArray("station"); databaseH.removeAll(tableName); for (int i = 0; i < rasp.length(); i++) { JSONObject oneObject = rasp.getJSONObject(i); String nameStation = oneObject.getString("name_station"); long codeStation = oneObject.getLong("id_station"); long sumStation = oneObject.getLong("sum_station"); databaseH.create(new AutoCompleteObject(nameStation, sumStation, codeStation), tableName); } SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(Constants.APP_PREFERENCES_MD5_EKB_CHECK, true); editor.apply(); if (Constants.LOG_ON) Log.v("Settings", "Station true"); } catch (JSONException e) { e.printStackTrace(); } return null; } } private class processingLoadScheduleResult extends AsyncTask<String, Void, List<RouteObjectResult>> { @Override protected List<RouteObjectResult> doInBackground(String... response) { JSONObject dataJsonQbj; List<RouteObjectResult> list = new ArrayList<>(); if (Constants.LOG_ON) Log.v(TAG, response[0]); try { dataJsonQbj = new JSONObject(response[0]); JSONArray rasp = dataJsonQbj.getJSONArray("rasp"); if (rasp.length() == 0) { runOnUiThread(new Runnable() { @Override @SuppressWarnings("ConstantConditions") public void run() { TextView textView1 = (TextView) findViewById(R.id.noItems); textView1.setVisibility(View.VISIBLE); } }); } else { runOnUiThread(new Runnable() { @Override @SuppressWarnings("ConstantConditions") public void run() { TextView textView1 = (TextView) findViewById(R.id.noItems); textView1.setVisibility(View.GONE); } }); } for (int i = 0; i < rasp.length(); i++) { JSONObject oneObject = rasp.getJSONObject(i); String timeMarsh = oneObject.getString("time_otpr"); String numberMarsh = oneObject.getString("new_number_data"); String numberMarshToSend = oneObject.getString("number_new_schedule"); String nameMarsh = oneObject.getString("name_data"); String nameBus = oneObject.getString("name_bus"); String countBus = oneObject.getString("count_bus"); String freeBus = oneObject.getString("free_place_new_schedule"); String priceBus = oneObject.getString("price_data"); String baggageBus = oneObject.getString("baggage_data"); String timePrib = oneObject.getString("time_prib_result"); int cancelBus = oneObject.getInt("cancel_new_schedule"); list.add(new RouteObjectResult(timeMarsh, numberMarsh, numberMarshToSend, nameMarsh, nameBus, countBus, freeBus, priceBus, baggageBus, timePrib, cancelBus)); } } catch (JSONException e) { e.printStackTrace(); } return list; } @Override protected void onPostExecute(List<RouteObjectResult> list) { final RouteObjectResultAdapter adapterResult = new RouteObjectResultAdapter(MainActivity.this, list); listView.setAdapter(adapterResult); super.onPostExecute(list); queryDialogDismiss(); } } private void queryDialogDismiss() { try { if (queryDialog != null && queryDialog.isShowing()) { queryDialog.dismiss(); } } catch (IllegalArgumentException e) { e.printStackTrace(); } finally { queryDialog = null; } } }