Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ultron.note;
- import android.content.*;
- import android.database.*;
- import android.graphics.*;
- import android.os.*;
- import android.preference.*;
- import android.support.design.widget.*;
- import android.support.v4.view.*;
- import android.support.v4.widget.*;
- import android.support.v7.app.*;
- import android.support.v7.widget.*;
- import android.util.*;
- import android.view.*;
- import android.widget.*;
- import com.getbase.floatingactionbutton.*;
- import com.ultron.note.fragments.*;
- import java.util.*;
- import android.support.v4.widget.CursorAdapter;
- import android.support.v7.widget.Toolbar;
- public class MainActivity extends AppCompatActivity
- implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
- private static final int CAT_ALL = -1;
- private static final String TAG_WELCOME_DIALOG = "welcome_dialog";
- FloatingActionsMenu fabMenu;
- //ArrayAdapter<String> adapter;
- private int selectedCategory = CAT_ALL; //ID of the currently selected category. Defaults to "all"
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.i("Tag", "1onCreate");
- SharedPreferences sp = MainActivity.this.getSharedPreferences("password", MainActivity.this.MODE_PRIVATE);
- String pwd = sp.getString("password", null);
- AppRater.app_launched(this);
- ListView notesList = (ListView) findViewById(R.id.notes_list);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- notesList.getAdapter().getCount();
- ActionBar actionBar = getSupportActionBar();
- actionBar.setSubtitle("Notas: " + notesList.size());
- //Toast.makeText(getApplicationContext(), "Total number of Items are:" + notesList.getAdapter().getCount() , Toast.LENGTH_LONG).show();
- //set the OnClickListeners
- findViewById(R.id.fab_text).setOnClickListener(this);
- findViewById(R.id.fab_checklist).setOnClickListener(this);
- findViewById(R.id.fab_audio).setOnClickListener(this);
- findViewById(R.id.fab_sketch).setOnClickListener(this);
- fabMenu = (FloatingActionsMenu) findViewById(R.id.fab_menu);
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- drawer.setDrawerListener(toggle);
- toggle.syncState();
- NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- navigationView.setNavigationItemSelectedListener(this);
- //Fill the list from database
- // arrayCountry.addAll(Arrays.asList(getResources().getStringArray(R.array.array_country)));
- notesList.setAdapter(new CursorAdapter(getApplicationContext(), DbAccess.getCursorAllNotes(getBaseContext()), CursorAdapter.FLAG_AUTO_REQUERY) {
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View rowView = inflater.inflate(R.layout.item_note, null);
- TextView text = (TextView) rowView.findViewById(R.id.item_name);
- String name = cursor.getString(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_NAME));
- if (name.length() >= 30) {
- text.setText(name.substring(0,27) + "...");
- } else {
- text.setText(name);
- }
- ImageView iv = (ImageView) rowView.findViewById(R.id.item_icon);
- switch (cursor.getInt(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_TYPE))) {
- case DbContract.NoteEntry.TYPE_SKETCH:
- iv.setColorFilter(R.color.color1);
- iv.setImageResource(R.drawable.ic_photo_black_24dp);
- break;
- case DbContract.NoteEntry.TYPE_AUDIO:
- iv.setImageResource(R.drawable.ic_mic_black_24dp);
- break;
- case DbContract.NoteEntry.TYPE_TEXT:
- iv.setImageResource(R.drawable.ic_short_text_black_24dp);
- break;
- case DbContract.NoteEntry.TYPE_CHECKLIST:
- iv.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
- break;
- default:
- }
- return rowView;
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- TextView text = (TextView) view.findViewById(R.id.item_name);
- String name = cursor.getString(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_NAME));
- if (name.length() >= 30) {
- text.setText(name.substring(0,27) + "...");
- } else {
- text.setText(name);
- }
- ImageView iv = (ImageView) view.findViewById(R.id.item_icon);
- switch (cursor.getInt(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_TYPE))) {
- case DbContract.NoteEntry.TYPE_SKETCH:
- iv.setImageResource(R.drawable.ic_photo_black_24dp);
- break;
- case DbContract.NoteEntry.TYPE_AUDIO:
- iv.setImageResource(R.drawable.ic_mic_black_24dp);
- break;
- case DbContract.NoteEntry.TYPE_TEXT:
- iv.setImageResource(R.drawable.ic_short_text_black_24dp);
- break;
- case DbContract.NoteEntry.TYPE_CHECKLIST:
- iv.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
- break;
- default:
- }
- }
- });
- notesList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
- notesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- //get details about the clicked note
- CursorAdapter ca = (CursorAdapter) parent.getAdapter();
- Cursor c = ca.getCursor();
- c.moveToPosition(position);
- //start the appropriate activity
- switch (c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_TYPE))) {
- case DbContract.NoteEntry.TYPE_TEXT:
- Intent i = new Intent(getApplication(), TextNoteActivity.class);
- i.putExtra(TextNoteActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
- startActivity(i);
- break;
- case DbContract.NoteEntry.TYPE_AUDIO:
- Intent i2 = new Intent(getApplication(), AudioNoteActivity.class);
- i2.putExtra(AudioNoteActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
- startActivity(i2);
- break;
- case DbContract.NoteEntry.TYPE_SKETCH:
- Intent i3 = new Intent(getApplication(), SketchActivity.class);
- i3.putExtra(SketchActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
- startActivity(i3);
- break;
- case DbContract.NoteEntry.TYPE_CHECKLIST:
- Intent i4 = new Intent(getApplication(), ChecklistNoteActivity.class);
- i4.putExtra(ChecklistNoteActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
- startActivity(i4);
- break;
- }
- }
- });
- notesList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
- @Override
- public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
- //do nothing
- }
- @Override
- public boolean onCreateActionMode(ActionMode mode, Menu menu) {
- // Inflate the menu for the CAB
- MenuInflater inflater = mode.getMenuInflater();
- inflater.inflate(R.menu.main_cab, menu);
- //Temporary fix, otherwise statusbar would be black
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
- getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
- // or Color.TRANSPARENT or your preferred color
- }
- return true;
- }
- @Override
- public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
- return false;
- }
- @Override
- public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
- // Respond to clicks on the actions in the CAB
- switch (item.getItemId()) {
- case R.id.action_delete:
- deleteSelectedItems();
- mode.finish(); // Action picked, so close the CAB
- Toast.makeText(getBaseContext(), R.string.movi, Toast.LENGTH_SHORT).show();
- return true;
- default:
- return false;
- }
- }
- @Override
- public void onDestroyActionMode(ActionMode mode) {
- //Temporary fix, otherwise statusbar would be black
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
- getWindow().setStatusBarColor(Color.TRANSPARENT);
- // or Color.TRANSPARENT or your preferred color
- }
- updateList();
- }
- });
- PreferenceManager.setDefaultValues(this, R.xml.pref_settings, false);
- if (sp.getBoolean(Preferences.SP_DATA_DISPLAY_WELCOME_DIALOG, true)) {
- WelcomeDialog welcomeDialog = new WelcomeDialog();
- welcomeDialog.show(getFragmentManager(), TAG_WELCOME_DIALOG);
- SharedPreferences.Editor editor = sp.edit();
- editor.putBoolean(Preferences.SP_DATA_DISPLAY_WELCOME_DIALOG, false);
- editor.commit();
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- updateList();
- buildDrawerMenu();
- }
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- super.onBackPressed();
- }}
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @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.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_sort_alphabetical) {
- //switch to an alphabetically sorted cursor.
- updateListAlphabetical();
- return true;
- } else if (id == R.id.action_help) {
- startActivity(new Intent(getApplication(), HelpActivity.class));
- } else if (id == R.id.action_settings) {
- startActivity(new Intent(getApplication(), SettingsActivity.class));
- } else if (id == R.id.action_about) {
- startActivity(new Intent(getApplication(), Sobre.class));
- }
- else if (id == R.id.webmenu) {
- Intent intent = new Intent(MainActivity.this, GuideGesturePasswordActivity.class);
- startActivity(intent);
- }
- return super.onOptionsItemSelected(item);
- }
- @SuppressWarnings("StatementWithEmptyBody")
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- // Handle navigation view item clicks here.
- item.setCheckable(true);
- item.setChecked(true);
- int id = item.getItemId();
- if (id == R.id.nav_trash) {
- startActivity(new Intent(getApplication(), RecycleActivity.class));
- } else if (id == R.id.nav_all) {
- selectedCategory = CAT_ALL;
- updateList();
- } else if (id == R.id.nav_manage_categories) {
- startActivity(new Intent(getApplication(), ManageCategoriesActivity.class));
- } else {
- selectedCategory = id;
- updateList();
- }
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- drawer.closeDrawer(GravityCompat.START);
- return true;
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.fab_text:
- startActivity(new Intent(getApplication(), TextNoteActivity.class));
- fabMenu.collapseImmediately();
- break;
- case R.id.fab_checklist:
- startActivity(new Intent(getApplication(), ChecklistNoteActivity.class));
- fabMenu.collapseImmediately();
- break;
- case R.id.fab_audio:
- startActivity(new Intent(getApplication(), AudioNoteActivity.class));
- fabMenu.collapseImmediately();
- break;
- case R.id.fab_sketch:
- startActivity(new Intent(getApplication(), SketchActivity.class));
- fabMenu.collapseImmediately();
- break;
- }
- }
- private void buildDrawerMenu() {
- NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- Menu navMenu = navigationView.getMenu();
- //reset the menu
- navMenu.clear();
- //Inflate the standard stuff
- MenuInflater menuInflater = new MenuInflater(getApplicationContext());
- menuInflater.inflate(R.menu.activity_main_drawer, navMenu);
- //Get the rest from the database
- Cursor c = DbAccess.getCategories(getBaseContext());
- while (c.moveToNext()){
- String name = c.getString(c.getColumnIndexOrThrow(DbContract.CategoryEntry.COLUMN_NAME));
- int id = c.getInt(c.getColumnIndexOrThrow(DbContract.CategoryEntry.COLUMN_ID));
- navMenu.add(R.id.drawer_group2, id, Menu.NONE, name).setIcon(R.drawable.ic_label_black_24dp);
- }
- c.close();
- }
- private void updateList() {
- ListView notesList = (ListView) findViewById(R.id.notes_list);
- CursorAdapter adapter = (CursorAdapter) notesList.getAdapter();
- if (selectedCategory == -1) { //show all
- String selection = DbContract.NoteEntry.COLUMN_TRASH + " = ?";
- String[] selectionArgs = { "0" };
- adapter.changeCursor(DbAccess.getCursorAllNotes(getBaseContext(), selection, selectionArgs));
- } else {
- String selection = DbContract.NoteEntry.COLUMN_CATEGORY + " = ? AND " + DbContract.NoteEntry.COLUMN_TRASH + " = ?";
- String[] selectionArgs = { String.valueOf(selectedCategory), "0" };
- adapter.changeCursor(DbAccess.getCursorAllNotes(getBaseContext(), selection, selectionArgs));
- }
- }
- private void updateListAlphabetical() {
- ListView notesList = (ListView) findViewById(R.id.notes_list);
- CursorAdapter adapter = (CursorAdapter) notesList.getAdapter();
- if (selectedCategory == -1) { //show all
- String selection = DbContract.NoteEntry.COLUMN_TRASH + " = ?";
- String[] selectionArgs = { "0" };
- adapter.changeCursor(DbAccess.getCursorAllNotesAlphabetical(getBaseContext(), selection, selectionArgs));
- } else {
- String selection = DbContract.NoteEntry.COLUMN_CATEGORY + " = ? AND " + DbContract.NoteEntry.COLUMN_TRASH + " = ?";
- String[] selectionArgs = { String.valueOf(selectedCategory), "0" };
- adapter.changeCursor(DbAccess.getCursorAllNotesAlphabetical(getBaseContext(), selection, selectionArgs));
- }
- }
- private void deleteSelectedItems(){
- ListView notesList = (ListView) findViewById(R.id.notes_list);
- CursorAdapter adapter = (CursorAdapter) notesList.getAdapter();
- SparseBooleanArray checkedItemPositions = notesList.getCheckedItemPositions();
- for (int i=0; i < checkedItemPositions.size(); i++) {
- if(checkedItemPositions.valueAt(i)) {
- DbAccess.trashNote(getBaseContext(), (int) (long) adapter.getItemId(checkedItemPositions.keyAt(i)));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement