Maintenance of the note function – private mode (the initial interface does not display the first line of the note)

I found that Xiaomi Notes automatically displays the first line of each note. When we use sticky notes in our daily lives, we often record some important (such as account passwords) or relatively private content in them out of convenience. Therefore, I plan to add a privacy mode to it – not showing the first line of the note and not showing the name of the folder (but the last modification time and alarm clock mark will be retained).

Table of Contents

1. Operation steps

2. Final effect

3. Summary


1. Operation steps

1. First of all, we need to know that we are adding the two options of “private mode” and “exit private mode” in the main interface, so first we need to add the two options in menu/note_list.xm Add two items to l, and then declare these two values in the three .xml files under values/strings/ (here Chinese simplified and traditional Chinese can be But just doing this is wrong. Let’s dig a hole here. You can think about why first and explain it later.

 <item
        android:id="@ + id/menu_secret"
        android:title="@string/menu_secret"/>

 <item
        android:id="@ + id/menu_quit_secret"
        android:title="@string/menu_quit_secret"/>

 <string name="menu_secret">secret model</string>
    <string name="menu_quit_secret">quit secret model</string>

 <string name="menu_secret">Private mode</string>
    <string name="menu_quit_secret">Exit private mode</string>

2. Then because we want to modify all the titles of the notes, we mainly add code to the NotesListActivity class. The first is to declare a variable secret_mode in the NotesListActivity() method. When its value is 0, it means that the private mode has not been entered, and when it is 1, it means that the private mode has been entered. Here I initialize it to 0.

 public static int secret_mode = 0;

Then set whether the two buttons are visible in the onPrepareOptionMenu() method. That is, the “Private Mode” button is not visible in private mode, and the “Exit Private Mode” button is not visible in non-private mode, so that the interface looks more logical.

 if(secret_mode == 1)
            menu.findItem(R.id.menu_secret).setVisible(false);
        else
            menu.findItem(R.id.menu_quit_secret).setVisible(false);

3. The next focus is the startAsyncNotesListQuery() method. From the previous code accuracy, we know that this function is mainly used for preparation before synchronizing the note list. In this method, what we want to modify is the fourth variable in the startQuery() function: NoteIemData.PROJECTION.

First, let’s take a look at what he has. With ctrl + left click, we enter the NoteItemData class. We can find that this is a string array, which stores the content of that row of titles! ! ! What we need to modify is SNIPPET! ! ! In this way the problem is solved.

public class NoteItemData {
    static final String [] PROJECTION = new String [] {
        NoteColumns.ID,
        NoteColumns.ALERTED_DATE,
        NoteColumns.BG_COLOR_ID,
        NoteColumns.CREATED_DATE,
        NoteColumns.HAS_ATTACHMENT,
        NoteColumns.MODIFIED_DATE,
        NoteColumns.NOTES_COUNT,
        NoteColumns.PARENT_ID,
        NoteColumns.SNIPPET,
        NoteColumns.TYPE,
        NoteColumns.WIDGET_ID,
        NoteColumns.WIDGET_TYPE,
    };

Modify the startAsyncNotesListQuery() method. Before initializing it, first judge the value of secret_mode. If it is 0, continue to call the PROJECTION array in NoteItemData according to the previous method; but if the value of secret_mode is 1, it enters the private mode at this time. Then we need to define a new PROJECTION array, then modify SNIPPET to the string we want to modify, and finally call this array.

 private void startAsyncNotesListQuery() {
        String selection = (mCurrentFolderId == Notes.ID_ROOT_FOLDER) ? ROOT_FOLDER_SELECTION
                : NORMAL_SELECTION;
        if(secret_mode == 0) {
            mBackgroundQueryHandler.startQuery(FOLDER_NOTE_LIST_QUERY_TOKEN, null,
                    Notes.CONTENT_NOTE_URI, NoteItemData.PROJECTION, selection, new String[]{
                            String.valueOf(mCurrentFolderId)
                    }, NoteColumns.TYPE + " DESC," + NoteColumns.MODIFIED_DATE + " DESC");
        }
        else{
            String str1 = "520";
            String [] PROJECTION = new String [] { //Define a new PROJECTION array and only replace SNIPPET
                    NoteColumns.ID,
                    NoteColumns.ALERTED_DATE,
                    NoteColumns.BG_COLOR_ID,
                    NoteColumns.CREATED_DATE,
                    NoteColumns.HAS_ATTACHMENT,
                    NoteColumns.MODIFIED_DATE,
                    NoteColumns.NOTES_COUNT,
                    NoteColumns.PARENT_ID,
// NoteColumns.SNIPPET,
                    str1,
                    NoteColumns.TYPE,
                    NoteColumns.WIDGET_ID,
                    NoteColumns.WIDGET_TYPE,
            };
            mBackgroundQueryHandler.startQuery(FOLDER_NOTE_LIST_QUERY_TOKEN, null,
                    Notes.CONTENT_NOTE_URI, PROJECTION, selection, new String[]{
                            String.valueOf(mCurrentFolderId)
                    }, NoteColumns.TYPE + " DESC," + NoteColumns.MODIFIED_DATE + " DESC");

        }
    }

4. Next, find the onOptionsItemSelected() method and add two new cases under switch(). In order to be more user-friendly here, I added the Toast and dialog methods.

 case R.id.menu_secret: { //Enter private mode
                secret_mode = 1;
                AlertDialog.Builder dialog = new AlertDialog.Builder(NotesListActivity.this);
                dialog.setTitle("Important Reminder");
                dialog.setMessage("Are you sure you want to enter private mode?");
                dialog.setCancelable(false);
                dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startAsyncNotesListQuery();
                       Toast.makeText(NotesListActivity.this,"You have entered private mode",Toast.LENGTH_SHORT).show();
                }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which){}
                });
                dialog.show();
                startAsyncNotesListQuery();
                Toast.makeText(this,"You have entered private mode",Toast.LENGTH_SHORT).show();
                break;
            }

 case R.id.menu_quit_secret:{ //Exit private mode
                secret_mode = 0;
                AlertDialog.Builder dialog = new AlertDialog.Builder(NotesListActivity.this);
                dialog.setTitle("Important Reminder");
                dialog.setMessage("Are you sure you want to exit private mode?");
                dialog.setCancelable(false);
                dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startAsyncNotesListQuery();
                        Toast.makeText(NotesListActivity.this,"You have exited private mode",Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which){}
                });
                dialog.show();
                break;
            }

Now that the code is almost written, we start testing this private mode. Clicking and clicking, we suddenly discovered that after entering the folder, if we click the menu button at this time, the program actually crashes! It says “Sorry, Sticky Notes has stopped running” and it keeps happening even after trying many times.

But don’t panic, AS actually provides a very powerful debug-like tool – logcat. We used logcat to check and locate the error, and found that the error was reported where the two buttons were switched. Thinking back to the menu of the folder just now, we can conclude that there should be something wrong with the menu under the folder.

Is it because there is no join button here? We tried adding the declarations of the previous two buttons in menu_sub_folder.xml.

 <item
        android:id="@ + id/menu_secret"
        android:title="@string/menu_secret"/>

    <item
        android:id="@ + id/menu_quit_secret"
        android:title="@string/menu_quit_secret"/>

After re-running, we found that the bug was resolved. So it should be that the menu under the folder was not considered before. Therefore, when we write code, we must consider all aspects as much as possible, otherwise it will be easy to make mistakes.

But it doesn’t matter if something goes wrong. Being good at using the logcat that comes with AS can help us solve many problems.

2. Final effect

3. Summary

In fact, when I first wrote this private mode function, I started from the NotesListAdapter class, but after writing for a long time, I found that it could not be solved because of the refresh problem. Later, after communicating with the boss, I changed my mind and was able to solve the problem. It is also recommended that when writing functions, you can communicate with classmates who have previously read the UI package for precise code. You should have unexpected gains.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 133673 people are learning the system