@Override protected void onResume() { super.onResume(); if (Utility.getPreferredLocation(this) != mlocation) { MainActivityFragment ff = new MainActivityFragment(); ff.onLocationChanged(); mlocation = Utility.getPreferredLocation(this); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocation = Utility.getPreferredLocation(this); setContentView(R.layout.activity_main); if (findViewById(R.id.weather_detail_container) != null) { // The detail container view will be present only in the large-screen layouts // (res/layout-sw600dp). If this view is present, then the activity should be // in two-pane mode. mTwoPane = true; // In two-pane mode, show the detail view in this activity by // adding or replacing the detail fragment using a // fragment transaction. if (savedInstanceState == null) { getSupportFragmentManager() .beginTransaction() .replace(R.id.weather_detail_container, new DetailFragment(), DETAILFRAGMENT_TAG) .commit(); } } else { mTwoPane = false; getSupportActionBar().setElevation(0f); } ForecastFragment forecastFragment = ((ForecastFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_forecast)); forecastFragment.setUseTodayLayout(!mTwoPane); }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { Intent intent = getActivity().getIntent(); if (intent == null || !intent.hasExtra(DetailActivity.DATE_KEY)) { return null; } String[] columns = { WeatherContract.WeatherEntry.TABLE_NAME + "." + WeatherContract.WeatherEntry._ID, WeatherContract.WeatherEntry.COLUMN_DATETEXT, WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, WeatherContract.WeatherEntry.COLUMN_HUMIDITY, WeatherContract.WeatherEntry.COLUMN_PRESSURE, WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, WeatherContract.WeatherEntry.COLUMN_DEGREES, WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, WeatherContract.LocationEntry.COLUMN_CITY_NAME }; String dateStr = getArguments().getString(DetailActivity.DATE_KEY); mLocation = Utility.getPreferredLocation(getActivity()); Uri weatherUri = WeatherContract.WeatherEntry.BuildWeatherLocationDate(mLocation, dateStr); return new CursorLoader(getActivity(), weatherUri, columns, null, null, null); }
@Override public void onResume() { super.onResume(); Bundle arguments = getArguments(); if (arguments != null && arguments.containsKey(DetailActivity.DATE_KEY) && mLocation != null && !mLocation.equals(Utility.getPreferredLocation(getActivity()))) { getLoaderManager().restartLoader(DETAIL_LOADER, null, this); } }
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { // Sort order: Ascending, by date. String sortOrder = Weathercontract.WeatherEntry.COLUMN_DATE + " ASC"; String locationSetting = Utility.getPreferredLocation(getActivity()); Uri weatherForLocationUri = Weathercontract.WeatherEntry.buildWeatherLocationWithStartDate( locationSetting, System.currentTimeMillis()); return new CursorLoader( getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocation = Utility.getPreferredLocation(this); if (savedInstanceState == null) { getFragmentManager() .beginTransaction() .add(R.id.container, new MainActivityFragment(), FORECASTFRAGMENT_TAG) .commit(); } }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String locationSettings = Utility.getPreferredLocation(getActivity()); // sort order Ascending by date String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate( locationSettings, System.currentTimeMillis()); return new CursorLoader( getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder); }
@Override public void onResume() { super.onResume(); String location = Utility.getPreferredLocation(this); // update the location in our second pane using the fragment manager if (location != null && !location.equals(mLocation)) { MainActivityFragment ff = (MainActivityFragment) getFragmentManager().findFragmentByTag(FORECASTFRAGMENT_TAG); if (null != ff) { ff.onLocationChanged(); } mLocation = location; } }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // Sort order: Ascending, by date. String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATETEXT + " ASC"; mLocation = Utility.getPreferredLocation(getActivity()); Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(mLocation, mDateStr); // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. return new CursorLoader( getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder); }
@Override protected void onResume() { super.onResume(); String location = Utility.getPreferredLocation(this); // update the location in our second pane using the fragment manager if (location != null && !location.equals(mLocation)) { ForecastFragment ff = (ForecastFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_forecast); if (null != ff) { ff.onLocationChanged(); } DetailFragment df = (DetailFragment) getSupportFragmentManager().findFragmentByTag(DETAILFRAGMENT_TAG); if (null != df) { df.onLocationChanged(location); } mLocation = location; } }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // This is called when a new Loader needs to be created. This // fragment only uses one loader, so we don't care about checking the id. // To only show current and future dates, filter the query to return weather only for // dates after or including today. String locationSettings = Utility.getPreferredLocation(getActivity()); String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate( locationSettings, System.currentTimeMillis()); Log.d(LOG_TAG, weatherForLocationUri.toString()); return new CursorLoader( getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder); }
private void openPreferredLocationMap() { Intent intent = new Intent(Intent.ACTION_VIEW); /* SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); String settingLocation = sharedPreferences.getString( getString(R.string.pref_location_key), getString(R.string.pref_location_default) ); */ String settingLocation = Utility.getPreferredLocation(this); // Uri geoLocation = Uri.parse("geo:0,0?q=" + settingLocation); Uri geoLocation = Uri.parse("geo:0,0?").buildUpon().appendQueryParameter("q", settingLocation).build(); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String locationSetting = Utility.getPreferredLocation(getActivity()); // Sort order: Ascending, by date. // String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; // Uri weatherForLocationUri = // WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate( // locationSetting, System.currentTimeMillis()); // Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri, // null, null, null, sortOrder); // The ForecastAdapter will take data from a source and // use it to populate the ListView it's attached to. // However, we cannot use FLAG_AUTO_REQUERY since it is deprecated, so we will end // up with an empty list the first time we run. // // The CursorAdapter will take data from our cursor and populate the ListView. // mForecastAdapter = new ForecastAdapter(getActivity(), null, 0); //// The ForecastAdapter will take data from a source and // // use it to populate the RecyclerView it's attached to. // mForecastAdapter = new ForecastAdapter(getActivity()); rootView = inflater.inflate(R.layout.fragment_main, container, false); // mListView = (ListView) rootView.findViewById(R.id.listview_forecast); // View emptyView = rootView.findViewById(R.id.listview_forecast_empty); // mListView.setEmptyView(emptyView); // mListView.setAdapter(mForecastAdapter); // Get a reference to the RecyclerView, and attach this adapter to it. mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_forecast); // Set the layout manager mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); View emptyView = rootView.findViewById(R.id.recyclerview_forecast_empty); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // The ForecastAdapter will take data from a source and // use it to populate the RecyclerView it's attached to. mForecastAdapter = new ForecastAdapter( getActivity(), new ForecastAdapter.ForecastAdapterOnClickHandler() { @Override public void onClick(Long date, ForecastAdapter.ForecastAdapterViewHolder vh) { String locationSetting = Utility.getPreferredLocation(getActivity()); ((Callback) getActivity()) .onItemSelected( WeatherContract.WeatherEntry.buildWeatherLocationWithDate( locationSetting, date), vh); // I could have sent the position back directly instead of the ViewHolder mPosition = vh.getAdapterPosition(); } }, emptyView, mChoiceMode); // specify an adapter (see also next example) mRecyclerView.setAdapter(mForecastAdapter); final View parallaxView = rootView.findViewById(R.id.parallax_bar); if (null != parallaxView) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mRecyclerView.addOnScrollListener( new RecyclerView.OnScrollListener() { @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int max = parallaxView.getHeight(); if (dy > 0) { parallaxView.setTranslationY( Math.max(-max, parallaxView.getTranslationY() - dy / 2)); } else { parallaxView.setTranslationY( Math.min(0, parallaxView.getTranslationY() - dy / 2)); } } }); } } final AppBarLayout appbarView = (AppBarLayout) rootView.findViewById(R.id.appbar); if (null != appbarView) { ViewCompat.setElevation(appbarView, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mRecyclerView.addOnScrollListener( new RecyclerView.OnScrollListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (0 == mRecyclerView.computeVerticalScrollOffset()) { appbarView.setElevation(0); } else { appbarView.setElevation(appbarView.getTargetElevation()); } } }); } } // mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { // // @Override // public void onItemClick(AdapterView adapterView, View view, int position, long l) // { // // CursorAdapter returns a cursor at the correct position for getItem(), or // null // // if it cannot seek to that position. // Cursor cursor = (Cursor) adapterView.getItemAtPosition(position); // if (cursor != null) { // String locationSetting = Utility.getPreferredLocation(getActivity()); // // ((Callback) getActivity()) // // .onItemSelected(WeatherContract.WeatherEntry.buildWeatherLocationWithDate( // locationSetting, cursor.getLong(COL_WEATHER_DATE) // )); // // mPosition = position; // } // } // }); // If there's instance state, mine it for useful information. // The end-goal here is that the user never knows that turning their device sideways // does crazy lifecycle related things. It should feel like some stuff stretched out, // or magically appeared to take advantage of room, but data or place in the app was never // actually *lost*. if (savedInstanceState != null) { if (savedInstanceState.containsKey(SELECTED_KEY)) { // The Recycler View probably hasn't even been populated yet. Actually perform the // swapout in onLoadFinished. mPosition = savedInstanceState.getInt(SELECTED_KEY); } mForecastAdapter.onRestoreInstanceState(savedInstanceState); } mForecastAdapter.setUseTodayLayout(mUseTodayLayout); return rootView; }
private void updateWeather() { FetchWeatherTask weatherTask = new FetchWeatherTask(getActivity()); String location = Utility.getPreferredLocation(getActivity()); weatherTask.execute(location); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocation = Utility.getPreferredLocation(this); Uri contentUri = getIntent() != null ? getIntent().getData() : null; setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar ab = getSupportActionBar(); if (ab != null) ab.setDisplayShowTitleEnabled(false); if (findViewById(R.id.weather_detail_container) != null) { // The detail container view will be present only in the large-screen layouts // (res/layout-sw600dp). If this view is present, then the activity should be // in two-pane mode mTwoPane = true; // In two-pane mode, show the detail view in this activity by // adding or replacing the detail fragment using a // fragment transaction. if (savedInstanceState == null) { DetailActivityFragment fragment = new DetailActivityFragment(); if (contentUri != null) { Bundle args = new Bundle(); args.putParcelable(DetailActivityFragment.DETAIL_URI, contentUri); fragment.setArguments(args); } getSupportFragmentManager() .beginTransaction() .replace(R.id.weather_detail_container, fragment, DETAILFRAGMENT_TAG) .commit(); } } else { mTwoPane = false; getSupportActionBar().setElevation(0f); } ForecastFragment forecastFragment = ((ForecastFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_forecast)); forecastFragment.setUseTodayLayout(!mTwoPane); if (contentUri != null) { forecastFragment.setInitialSelectedDate( WeatherContract.WeatherEntry.getDateFromUri(contentUri)); } SunshineSyncAdapter.initializeSyncAdapter(this); // If Google Play Services is up to date, we'll want to register GCM. If it is not, we'll // skip the registration and this device will not receive any downstream messages from // our fake server. Because weather alerts are not a core feature of the app, this should // not affect the behavior of the app, from a user perspective. if (checkPlayServices()) { // Because this is the initial creation of the app, we'll want to be certain we have // a token. If we do not, then we will start the IntentService that will register this // application with GCM. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); boolean sentToken = sharedPreferences.getBoolean(SENT_TOKEN_TO_SERVER, false); if (!sentToken) { Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } } }