Java tutorial
/* * * Copyright 2016 Michael A Updike * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.weebly.opus1269.copyeverywhere.ui.devices; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.NavUtils; import android.support.v4.content.ContextCompat; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import com.weebly.opus1269.copyeverywhere.R; import com.weebly.opus1269.copyeverywhere.cloud.UpstreamMessages; import com.weebly.opus1269.copyeverywhere.helpers.AppUtils; import com.weebly.opus1269.copyeverywhere.model.Devices; import com.weebly.opus1269.copyeverywhere.ui.shared.MenuTint; public class DevicesActivity extends AppCompatActivity { private static final String TAG = "DevicesActivity"; // Adapter being used to display the list's data private DevicesAdapter mAdapter = null; private BroadcastReceiver mDevicesReceiver = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_devices); final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.deviceList); if (recyclerView != null) { mAdapter = new DevicesAdapter(); recyclerView.setAdapter(mAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); if (fab != null) { fab.hide(); } final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); } // handler for received Intents for the "devices" event mDevicesReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { notifyAdapter(intent); } private void notifyAdapter(Intent intent) { final Bundle bundle = intent.getBundleExtra(Devices.BUNDLE); final String action = bundle.getString(Devices.ACTION); final int pos = bundle.getInt(Devices.POS); if (action == null) { return; } AppUtils.log(TAG, "Devices change: " + action + " pos: " + pos); switch (action) { case Devices.ACTION_ADD: mAdapter.notifyItemInserted(pos); break; case Devices.ACTION_REMOVE: mAdapter.notifyItemRemoved(pos); break; case Devices.ACTION_CLEAR: mAdapter.notifyDataSetChanged(); break; default: break; } } }; } @Override protected void onStart() { super.onStart(); // ping devices UpstreamMessages.sendPing(); } @Override protected void onPause() { super.onPause(); // Unregister since the activity is not visible LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevicesReceiver); super.onPause(); } @Override protected void onResume() { super.onResume(); // Register mDevicesReceiver to receive messages. LocalBroadcastManager.getInstance(this).registerReceiver(mDevicesReceiver, new IntentFilter(Devices.INTENT_FILTER)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_devices, menu); final int color = ContextCompat.getColor(this, R.color.icons); final int alpha = 255; MenuTint.on(menu).setMenuItemIconColor(color).setMenuItemIconAlpha(alpha).apply(this); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. boolean processed = true; final int id = item.getItemId(); switch (id) { case R.id.home: NavUtils.navigateUpFromSameTask(this); break; case R.id.action_refresh: doRefresh(); break; case R.id.action_delete: // showDeleteDialog(); break; default: processed = false; break; } return processed || super.onOptionsItemSelected(item); } private static void doRefresh() { UpstreamMessages.sendPing(); } }