Search in sources :

Example 21 with Prefs

use of com.a5corp.weather.preferences.Prefs in project Weather by Sparker0i.

the class MyApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    Configuration config = getBaseContext().getResources().getConfiguration();
    String lang = preferences.getString(getString(R.string.pref_language), "en");
    locale = new Locale(lang);
    config.setLocale(locale);
    Log.i("Locale", lang);
    Locale.setDefault(locale);
    updateConfiguration(config);
    setSystemLocale(config, locale);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
        LanguageUtil.setLanguage(this, new Prefs(this).getLanguage());
}
Also used : Locale(java.util.Locale) Configuration(android.content.res.Configuration) SharedPreferences(android.content.SharedPreferences) Prefs(com.a5corp.weather.preferences.Prefs)

Example 22 with Prefs

use of com.a5corp.weather.preferences.Prefs 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 23 with Prefs

use of com.a5corp.weather.preferences.Prefs in project Weather by Sparker0i.

the class AlarmReceiver method setRecurringAlarm.

public static void setRecurringAlarm(Context context) {
    Intent refresh = new Intent(context, AlarmReceiver.class);
    PendingIntent recurringRefresh = PendingIntent.getBroadcast(context, 0, refresh, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long intervalMillis = Long.parseLong(new Prefs(context).getTime());
    if (intervalMillis == 0) {
        // Cancel previous alarm
        alarms.cancel(recurringRefresh);
    } else {
        alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + intervalMillis, intervalMillis, recurringRefresh);
    }
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Prefs(com.a5corp.weather.preferences.Prefs)

Example 24 with Prefs

use of com.a5corp.weather.preferences.Prefs 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 25 with Prefs

use of com.a5corp.weather.preferences.Prefs in project Weather by Sparker0i.

the class Request method gsonWeather.

private WeatherInfo gsonWeather(URL url) throws IOException {
    HttpURLConnection connection1 = (HttpURLConnection) url.openConnection();
    connection1.addRequestProperty("x-api-key", new Prefs(context).getWeatherKey());
    InputStream content = connection1.getInputStream();
    try {
        // Read the server response and attempt to parse it as JSON
        Reader reader = new InputStreamReader(content);
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("M/d/yy hh:mm a");
        Gson gson = gsonBuilder.create();
        WeatherInfo posts = gson.fromJson(reader, WeatherInfo.class);
        System.out.println(gson.toJson(posts));
        content.close();
        return posts;
    } catch (Exception ex) {
        Log.e("FetchWeather", "Failed to parse JSON due to: " + ex);
    }
    return null;
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) Prefs(com.a5corp.weather.preferences.Prefs) IOException(java.io.IOException)

Aggregations

Prefs (com.a5corp.weather.preferences.Prefs)25 AlarmManager (android.app.AlarmManager)6 Intent (android.content.Intent)6 PendingIntent (android.app.PendingIntent)5 Context (android.content.Context)5 WeatherInfo (com.a5corp.weather.model.WeatherInfo)3 Date (java.util.Date)3 Bundle (android.os.Bundle)2 View (android.view.View)2 WeatherActivity (com.a5corp.weather.activity.WeatherActivity)2 WeatherFragment (com.a5corp.weather.fragment.WeatherFragment)2 CheckConnection (com.a5corp.weather.internet.CheckConnection)2 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Locale (java.util.Locale)2 SharedPreferences (android.content.SharedPreferences)1 Configuration (android.content.res.Configuration)1 Handler (android.os.Handler)1 KeyEvent (android.view.KeyEvent)1 Button (android.widget.Button)1