Search in sources :

Example 1 with CheckConnection

use of com.a5corp.weather.internet.CheckConnection in project Weather by Sparker0i.

the class WeatherFragment method updateWeatherData.

private void updateWeatherData(final String city, final String lat, final String lon) {
    wt = new FetchWeather(context());
    if (citys == null)
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                fabProgressCircle.show();
            }
        }, 50);
    new Thread() {

        public void run() {
            try {
                if (lat == null && lon == null) {
                    json = wt.execute(citys != null ? citys : city).get();
                } else if (city == null) {
                    json = wt.execute(lat, lon).get();
                }
            } catch (InterruptedException iex) {
                Log.e("InterruptedException", "iex");
            } catch (ExecutionException eex) {
                Log.e("ExecutionException", "eex");
            }
            if (pd.isShowing())
                pd.dismiss();
            if (json == null) {
                preferences.setCity(preferences.getLastCity());
                handler.post(new Runnable() {

                    public void run() {
                        GlobalActivity.i = 1;
                        if (!preferences.getLaunched()) {
                            FirstStart();
                        } else {
                            if (citys == null)
                                fabProgressCircle.hide();
                            cc = new CheckConnection(context());
                            if (!cc.isNetworkAvailable()) {
                                showNoInternet();
                            } else {
                                if (pd.isShowing())
                                    pd.dismiss();
                                showInputDialog();
                            }
                        }
                    }
                });
            } else {
                handler.post(new Runnable() {

                    public void run() {
                        preferences.setLaunched();
                        renderWeather(json);
                        if (!preferences.getv3TargetShown())
                            showTargets();
                        if (pd.isShowing())
                            pd.dismiss();
                        if (citys == null) {
                            preferences.setLastCity(json.day.getName() + "," + json.day.getSys().getCountry());
                            ((WeatherActivity) activity()).createShortcuts();
                            progress();
                        } else
                            preferences.setLastCity(preferences.getLastCity());
                        NotificationService.enqueueWork(context(), new Intent(context(), WeatherActivity.class));
                    }
                });
            }
        }
    }.start();
}
Also used : FetchWeather(com.a5corp.weather.internet.FetchWeather) Intent(android.content.Intent) WeatherActivity(com.a5corp.weather.activity.WeatherActivity) ExecutionException(java.util.concurrent.ExecutionException) CheckConnection(com.a5corp.weather.internet.CheckConnection)

Example 2 with CheckConnection

use of com.a5corp.weather.internet.CheckConnection in project Weather by Sparker0i.

the class FirstLaunchFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_first_launch, container, false);
    preferences = new Prefs(getContext());
    cityInput = rootView.findViewById(R.id.city_input);
    textField = rootView.findViewById(R.id.materialTextField);
    ImageView img = textField.findViewById(R.id.mtf_image);
    img.setImageAlpha(R.drawable.logo);
    img.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            permission = new Permissions(getContext());
            requestPermissions(new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION }, Constants.READ_COARSE_LOCATION);
        }
    });
    message = rootView.findViewById(R.id.intro_text);
    if (GlobalActivity.i == 0) {
        message.setText(getString(R.string.pick_city));
    } else {
        message.setText(getString(R.string.uh_oh));
    }
    Button goButton = rootView.findViewById(R.id.go_button);
    goButton.setText(getString(android.R.string.ok));
    goButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!new CheckConnection(getContext()).isNetworkAvailable()) {
                Snackbar.make(rootView, getString(R.string.check_internet), Snackbar.LENGTH_SHORT).show();
            } else if (cityInput.getText().length() > 0) {
                launchActivity(0);
            } else {
                Snackbar.make(rootView, getString(R.string.enter_city_first), Snackbar.LENGTH_SHORT).show();
            }
        }
    });
    cityInput.setOnEditorActionListener(new EditText.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                launchActivity(0);
                return true;
            }
            return false;
        }
    });
    return rootView;
}
Also used : EditText(android.widget.EditText) Prefs(com.a5corp.weather.preferences.Prefs) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) KeyEvent(android.view.KeyEvent) Button(android.widget.Button) Permissions(com.a5corp.weather.permissions.Permissions) TextView(android.widget.TextView) ImageView(android.widget.ImageView) CheckConnection(com.a5corp.weather.internet.CheckConnection)

Example 3 with CheckConnection

use of com.a5corp.weather.internet.CheckConnection in project Weather by Sparker0i.

the class NotificationService method onHandleWork.

@Override
protected void onHandleWork(@NonNull Intent intent) {
    CheckConnection checkNetwork = new CheckConnection(this);
    if (!checkNetwork.isNetworkAvailable()) {
        return;
    }
    Log.i("In", "Notification Service Alarm");
    intent = NotificationService.newIntent(this);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    prefs = new Prefs(this);
    long intervalMillis = Long.parseLong(prefs.getTime());
    if (alarmManager != null)
        if (new Prefs(this).getNotifs()) {
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), intervalMillis, pendingIntent);
        } else {
            alarmManager.cancel(pendingIntent);
            pendingIntent.cancel();
        }
    String city = prefs.getCity();
    String units = PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.PREF_TEMPERATURE_UNITS, Constants.METRIC);
    try {
        WeatherInfo weather;
        weather = new Request(this).getItems(city, units);
        if (new Prefs(this).getNotifs())
            weatherNotification(weather);
    } catch (IOException e) {
        Log.e(TAG, "Error get weather", e);
    }
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) Request(com.a5corp.weather.internet.Request) AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) Prefs(com.a5corp.weather.preferences.Prefs) CheckConnection(com.a5corp.weather.internet.CheckConnection)

Example 4 with CheckConnection

use of com.a5corp.weather.internet.CheckConnection in project Weather by Sparker0i.

the class WeatherFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        cc = new CheckConnection(context());
        if (!cc.isNetworkAvailable())
            showNoInternet();
        else {
            pd.show();
            updateWeatherData(preferences.getCity(), null, null);
        }
    }
}
Also used : CheckConnection(com.a5corp.weather.internet.CheckConnection)

Aggregations

CheckConnection (com.a5corp.weather.internet.CheckConnection)4 Prefs (com.a5corp.weather.preferences.Prefs)2 AlarmManager (android.app.AlarmManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 KeyEvent (android.view.KeyEvent)1 View (android.view.View)1 Button (android.widget.Button)1 EditText (android.widget.EditText)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 WeatherActivity (com.a5corp.weather.activity.WeatherActivity)1 FetchWeather (com.a5corp.weather.internet.FetchWeather)1 Request (com.a5corp.weather.internet.Request)1 WeatherInfo (com.a5corp.weather.model.WeatherInfo)1 Permissions (com.a5corp.weather.permissions.Permissions)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1