Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ultron.note;
  2.  
  3. import android.content.*;
  4. import android.database.*;
  5. import android.graphics.*;
  6. import android.os.*;
  7. import android.preference.*;
  8. import android.support.design.widget.*;
  9. import android.support.v4.view.*;
  10. import android.support.v4.widget.*;
  11. import android.support.v7.app.*;
  12. import android.support.v7.widget.*;
  13. import android.util.*;
  14. import android.view.*;
  15. import android.widget.*;
  16. import com.getbase.floatingactionbutton.*;
  17. import com.ultron.note.fragments.*;
  18. import java.util.*;
  19.  
  20. import android.support.v4.widget.CursorAdapter;
  21. import android.support.v7.widget.Toolbar;
  22.  
  23. public class MainActivity extends AppCompatActivity
  24.         implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
  25.  
  26.    private static final int CAT_ALL = -1;
  27.     private static final String TAG_WELCOME_DIALOG = "welcome_dialog";
  28.     FloatingActionsMenu fabMenu;
  29.     //ArrayAdapter<String> adapter;
  30.     private int selectedCategory = CAT_ALL; //ID of the currently selected category. Defaults to "all"
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.    
  35.        
  36.        
  37.         Log.i("Tag", "1onCreate");
  38.         SharedPreferences sp = MainActivity.this.getSharedPreferences("password", MainActivity.this.MODE_PRIVATE);
  39.         String pwd = sp.getString("password", null);
  40.      
  41.         AppRater.app_launched(this);
  42.  
  43.         ListView notesList = (ListView) findViewById(R.id.notes_list);
  44.        
  45.         setContentView(R.layout.activity_main);
  46.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  47.         setSupportActionBar(toolbar);
  48.         notesList.getAdapter().getCount();
  49.        
  50.         ActionBar actionBar = getSupportActionBar();
  51.         actionBar.setSubtitle("Notas: " +  notesList.size());
  52.        
  53.         //Toast.makeText(getApplicationContext(), "Total number of Items are:" + notesList.getAdapter().getCount() , Toast.LENGTH_LONG).show();
  54.         //set the OnClickListeners
  55.         findViewById(R.id.fab_text).setOnClickListener(this);
  56.         findViewById(R.id.fab_checklist).setOnClickListener(this);
  57.         findViewById(R.id.fab_audio).setOnClickListener(this);
  58.         findViewById(R.id.fab_sketch).setOnClickListener(this);
  59.  
  60.         fabMenu = (FloatingActionsMenu) findViewById(R.id.fab_menu);
  61.  
  62.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  63.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  64.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  65.         drawer.setDrawerListener(toggle);
  66.         toggle.syncState();
  67.  
  68.         NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  69.         navigationView.setNavigationItemSelectedListener(this);
  70.  
  71.         //Fill the list from database
  72.      
  73.  
  74.         //  arrayCountry.addAll(Arrays.asList(getResources().getStringArray(R.array.array_country)));
  75.         notesList.setAdapter(new CursorAdapter(getApplicationContext(), DbAccess.getCursorAllNotes(getBaseContext()), CursorAdapter.FLAG_AUTO_REQUERY) {
  76.          
  77.             @Override
  78.             public View newView(Context context, Cursor cursor, ViewGroup parent) {
  79.                 LayoutInflater inflater = (LayoutInflater) context
  80.                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  81.                 View rowView = inflater.inflate(R.layout.item_note, null);
  82.  
  83.                 TextView text = (TextView) rowView.findViewById(R.id.item_name);
  84.                 String name = cursor.getString(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_NAME));
  85.                 if (name.length() >= 30) {
  86.                     text.setText(name.substring(0,27) + "...");
  87.                 } else {
  88.                     text.setText(name);
  89.                 }
  90.                
  91.  
  92.                 ImageView iv = (ImageView) rowView.findViewById(R.id.item_icon);
  93.                 switch (cursor.getInt(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_TYPE))) {
  94.                     case DbContract.NoteEntry.TYPE_SKETCH:
  95.                        iv.setColorFilter(R.color.color1);
  96.                         iv.setImageResource(R.drawable.ic_photo_black_24dp);
  97.                         break;
  98.                     case DbContract.NoteEntry.TYPE_AUDIO:
  99.                         iv.setImageResource(R.drawable.ic_mic_black_24dp);
  100.                         break;
  101.                     case DbContract.NoteEntry.TYPE_TEXT:
  102.                         iv.setImageResource(R.drawable.ic_short_text_black_24dp);
  103.                         break;
  104.                     case DbContract.NoteEntry.TYPE_CHECKLIST:
  105.                         iv.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
  106.                         break;
  107.                     default:
  108.                 }
  109.                 return rowView;
  110.             }
  111.  
  112.             @Override
  113.             public void bindView(View view, Context context, Cursor cursor) {
  114.                 TextView text = (TextView) view.findViewById(R.id.item_name);
  115.                 String name = cursor.getString(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_NAME));
  116.                 if (name.length() >= 30) {
  117.                     text.setText(name.substring(0,27) + "...");
  118.                 } else {
  119.                     text.setText(name);
  120.                 }
  121.  
  122.                 ImageView iv = (ImageView) view.findViewById(R.id.item_icon);
  123.                 switch (cursor.getInt(cursor.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_TYPE))) {
  124.                     case DbContract.NoteEntry.TYPE_SKETCH:
  125.                         iv.setImageResource(R.drawable.ic_photo_black_24dp);
  126.                         break;
  127.                     case DbContract.NoteEntry.TYPE_AUDIO:
  128.                         iv.setImageResource(R.drawable.ic_mic_black_24dp);
  129.                         break;
  130.                     case DbContract.NoteEntry.TYPE_TEXT:
  131.                         iv.setImageResource(R.drawable.ic_short_text_black_24dp);
  132.                         break;
  133.                     case DbContract.NoteEntry.TYPE_CHECKLIST:
  134.                         iv.setImageResource(R.drawable.ic_format_list_bulleted_black_24dp);
  135.                         break;
  136.                     default:
  137.                 }
  138.             }
  139.         });
  140.         notesList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
  141.         notesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  142.             @Override
  143.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  144.                 //get details about the clicked note
  145.                 CursorAdapter ca = (CursorAdapter) parent.getAdapter();
  146.                 Cursor c = ca.getCursor();
  147.                 c.moveToPosition(position);
  148.                 //start the appropriate activity
  149.                 switch (c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_TYPE))) {
  150.                     case DbContract.NoteEntry.TYPE_TEXT:
  151.                         Intent i = new Intent(getApplication(), TextNoteActivity.class);
  152.                         i.putExtra(TextNoteActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
  153.                         startActivity(i);
  154.                         break;
  155.                     case DbContract.NoteEntry.TYPE_AUDIO:
  156.                         Intent i2 = new Intent(getApplication(), AudioNoteActivity.class);
  157.                         i2.putExtra(AudioNoteActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
  158.                         startActivity(i2);
  159.                         break;
  160.                     case DbContract.NoteEntry.TYPE_SKETCH:
  161.                         Intent i3 = new Intent(getApplication(), SketchActivity.class);
  162.                         i3.putExtra(SketchActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
  163.                         startActivity(i3);
  164.                         break;
  165.                     case DbContract.NoteEntry.TYPE_CHECKLIST:
  166.                         Intent i4 = new Intent(getApplication(), ChecklistNoteActivity.class);
  167.                         i4.putExtra(ChecklistNoteActivity.EXTRA_ID, c.getInt(c.getColumnIndexOrThrow(DbContract.NoteEntry.COLUMN_ID)));
  168.                         startActivity(i4);
  169.                         break;
  170.                 }
  171.             }
  172.         });
  173.         notesList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
  174.             @Override
  175.             public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
  176.                 //do nothing
  177.             }
  178.  
  179.             @Override
  180.             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  181.                 // Inflate the menu for the CAB
  182.                 MenuInflater inflater = mode.getMenuInflater();
  183.                 inflater.inflate(R.menu.main_cab, menu);
  184.                 //Temporary fix, otherwise statusbar would be black
  185.                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
  186.                     getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
  187.                     // or Color.TRANSPARENT or your preferred color
  188.                 }
  189.                 return true;
  190.             }
  191.  
  192.             @Override
  193.             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  194.                 return false;
  195.             }
  196.  
  197.             @Override
  198.             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  199.                 // Respond to clicks on the actions in the CAB
  200.                 switch (item.getItemId()) {
  201.                     case R.id.action_delete:
  202.                         deleteSelectedItems();
  203.                         mode.finish(); // Action picked, so close the CAB
  204.                         Toast.makeText(getBaseContext(), R.string.movi, Toast.LENGTH_SHORT).show();
  205.                        
  206.                         return true;
  207.                     default:
  208.                         return false;
  209.                 }
  210.             }
  211.  
  212.             @Override
  213.             public void onDestroyActionMode(ActionMode mode) {
  214.                 //Temporary fix, otherwise statusbar would be black
  215.                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
  216.                     getWindow().setStatusBarColor(Color.TRANSPARENT);
  217.                     // or Color.TRANSPARENT or your preferred color
  218.                 }
  219.                 updateList();
  220.             }
  221.         });
  222.  
  223.  
  224.         PreferenceManager.setDefaultValues(this, R.xml.pref_settings, false);
  225.             if (sp.getBoolean(Preferences.SP_DATA_DISPLAY_WELCOME_DIALOG, true)) {
  226.             WelcomeDialog welcomeDialog = new WelcomeDialog();
  227.             welcomeDialog.show(getFragmentManager(), TAG_WELCOME_DIALOG);
  228.             SharedPreferences.Editor editor = sp.edit();
  229.             editor.putBoolean(Preferences.SP_DATA_DISPLAY_WELCOME_DIALOG, false);
  230.             editor.commit();
  231.         }
  232.     }
  233.  
  234.     @Override
  235.     protected void onResume() {
  236.         super.onResume();
  237.         updateList();
  238.         buildDrawerMenu();
  239.     }
  240.  
  241.     @Override
  242.     public void onBackPressed() {
  243.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  244.         if (drawer.isDrawerOpen(GravityCompat.START)) {
  245.             drawer.closeDrawer(GravityCompat.START);
  246.         } else {
  247.             super.onBackPressed();
  248.        
  249.    
  250.     }}
  251.    
  252.     @Override
  253.     public boolean onCreateOptionsMenu(Menu menu) {
  254.         // Inflate the menu; this adds items to the action bar if it is present.
  255.         getMenuInflater().inflate(R.menu.main, menu);
  256.         return true;
  257.     }
  258.  
  259.     @Override
  260.     public boolean onOptionsItemSelected(MenuItem item) {
  261.         // Handle action bar item clicks here. The action bar will
  262.         // automatically handle clicks on the Home/Up button, so long
  263.         // as you specify a parent activity in AndroidManifest.xml.
  264.         int id = item.getItemId();
  265.  
  266.        
  267.        
  268.         //noinspection SimplifiableIfStatement
  269.         if (id == R.id.action_sort_alphabetical) {
  270.             //switch to an alphabetically sorted cursor.
  271.             updateListAlphabetical();
  272.             return true;
  273.         } else if (id == R.id.action_help) {
  274.             startActivity(new Intent(getApplication(), HelpActivity.class));
  275.         } else if (id == R.id.action_settings) {
  276.             startActivity(new Intent(getApplication(), SettingsActivity.class));
  277.         } else if (id == R.id.action_about) {
  278.             startActivity(new Intent(getApplication(), Sobre.class));
  279.         }
  280.     else if (id == R.id.webmenu) {
  281.        
  282.  
  283.         Intent intent = new Intent(MainActivity.this, GuideGesturePasswordActivity.class);
  284.     startActivity(intent);
  285. }
  286.    
  287.    
  288.         return super.onOptionsItemSelected(item);
  289.     }
  290.  
  291.     @SuppressWarnings("StatementWithEmptyBody")
  292.     @Override
  293.     public boolean onNavigationItemSelected(MenuItem item) {
  294.         // Handle navigation view item clicks here.
  295.         item.setCheckable(true);
  296.         item.setChecked(true);
  297.         int id = item.getItemId();
  298.         if (id == R.id.nav_trash) {
  299.             startActivity(new Intent(getApplication(), RecycleActivity.class));
  300.         } else if (id == R.id.nav_all) {
  301.             selectedCategory = CAT_ALL;
  302.             updateList();
  303.         } else if (id == R.id.nav_manage_categories) {
  304.             startActivity(new Intent(getApplication(), ManageCategoriesActivity.class));
  305.         } else {
  306.             selectedCategory = id;
  307.             updateList();
  308.         }
  309.  
  310.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  311.         drawer.closeDrawer(GravityCompat.START);
  312.         return true;
  313.     }
  314.  
  315.     @Override
  316.     public void onClick(View v) {
  317.         switch (v.getId()) {
  318.             case R.id.fab_text:
  319.                 startActivity(new Intent(getApplication(), TextNoteActivity.class));
  320.                 fabMenu.collapseImmediately();
  321.                 break;
  322.             case R.id.fab_checklist:
  323.                 startActivity(new Intent(getApplication(), ChecklistNoteActivity.class));
  324.                 fabMenu.collapseImmediately();
  325.                 break;
  326.             case R.id.fab_audio:
  327.                 startActivity(new Intent(getApplication(), AudioNoteActivity.class));
  328.                 fabMenu.collapseImmediately();
  329.                 break;
  330.             case R.id.fab_sketch:
  331.                 startActivity(new Intent(getApplication(), SketchActivity.class));
  332.                 fabMenu.collapseImmediately();
  333.                 break;
  334.         }
  335.     }
  336.  
  337.     private void buildDrawerMenu() {
  338.         NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  339.         Menu navMenu = navigationView.getMenu();
  340.         //reset the menu
  341.         navMenu.clear();
  342.         //Inflate the standard stuff
  343.         MenuInflater menuInflater = new MenuInflater(getApplicationContext());
  344.         menuInflater.inflate(R.menu.activity_main_drawer, navMenu);
  345.         //Get the rest from the database
  346.         Cursor c = DbAccess.getCategories(getBaseContext());
  347.         while (c.moveToNext()){
  348.             String name = c.getString(c.getColumnIndexOrThrow(DbContract.CategoryEntry.COLUMN_NAME));
  349.             int id = c.getInt(c.getColumnIndexOrThrow(DbContract.CategoryEntry.COLUMN_ID));
  350.             navMenu.add(R.id.drawer_group2, id, Menu.NONE, name).setIcon(R.drawable.ic_label_black_24dp);
  351.         }
  352.         c.close();
  353.     }
  354.  
  355.     private void updateList() {
  356.         ListView notesList = (ListView) findViewById(R.id.notes_list);
  357.         CursorAdapter adapter = (CursorAdapter) notesList.getAdapter();
  358.         if (selectedCategory == -1) { //show all
  359.             String selection = DbContract.NoteEntry.COLUMN_TRASH + " = ?";
  360.             String[] selectionArgs = { "0" };
  361.             adapter.changeCursor(DbAccess.getCursorAllNotes(getBaseContext(), selection, selectionArgs));
  362.         } else {
  363.             String selection = DbContract.NoteEntry.COLUMN_CATEGORY + " = ? AND " + DbContract.NoteEntry.COLUMN_TRASH + " = ?";
  364.             String[] selectionArgs = { String.valueOf(selectedCategory), "0" };
  365.             adapter.changeCursor(DbAccess.getCursorAllNotes(getBaseContext(), selection, selectionArgs));
  366.         }
  367.     }
  368.  
  369.     private void updateListAlphabetical() {
  370.         ListView notesList = (ListView) findViewById(R.id.notes_list);
  371.         CursorAdapter adapter = (CursorAdapter) notesList.getAdapter();
  372.         if (selectedCategory == -1) { //show all
  373.             String selection = DbContract.NoteEntry.COLUMN_TRASH + " = ?";
  374.             String[] selectionArgs = { "0" };
  375.             adapter.changeCursor(DbAccess.getCursorAllNotesAlphabetical(getBaseContext(), selection, selectionArgs));
  376.         } else {
  377.             String selection = DbContract.NoteEntry.COLUMN_CATEGORY + " = ? AND " + DbContract.NoteEntry.COLUMN_TRASH + " = ?";
  378.             String[] selectionArgs = { String.valueOf(selectedCategory), "0" };
  379.             adapter.changeCursor(DbAccess.getCursorAllNotesAlphabetical(getBaseContext(), selection, selectionArgs));
  380.         }
  381.     }
  382.  
  383.     private void deleteSelectedItems(){
  384.         ListView notesList = (ListView) findViewById(R.id.notes_list);
  385.         CursorAdapter adapter = (CursorAdapter) notesList.getAdapter();
  386.         SparseBooleanArray checkedItemPositions = notesList.getCheckedItemPositions();
  387.         for (int i=0; i < checkedItemPositions.size(); i++) {
  388.             if(checkedItemPositions.valueAt(i)) {
  389.                 DbAccess.trashNote(getBaseContext(), (int) (long) adapter.getItemId(checkedItemPositions.keyAt(i)));
  390.             }
  391.         }
  392.     }
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement