Skip to content

Commit

Permalink
add del
Browse files Browse the repository at this point in the history
  • Loading branch information
silvernoo committed Aug 26, 2016
1 parent 6438c6f commit da6eb4a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.github.silvernoo.browserbridge;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.github.silvernoo.browserbridge.dao.ShareContentDao;
import com.github.silvernoo.browserbridge.ext.ItemTouchHelperViewHolder;
import com.github.silvernoo.browserbridge.ext.MyListCursorAdapter;
import com.github.silvernoo.browserbridge.widget.SimpleDividerItemDecoration;

Expand All @@ -24,6 +28,8 @@ public class MainActivity extends BaseActivity {

private ShareContentDao shareContentDao;
private MyListCursorAdapter mAdapter;
public static final float ALPHA_FULL = 1.0f;
public Cursor cursor;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -45,17 +51,70 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mRecyclerView.setLayoutManager(mLayoutManager);

// specify an adapter (see also next example)
mAdapter = new MyListCursorAdapter(this, shareContentDao.preLoad());
mAdapter = new MyListCursorAdapter(this, cursor = shareContentDao.preLoad());
mRecyclerView.setAdapter(mAdapter);

Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.Settings$UsageAccessSettingsActivity");
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
MyListCursorAdapter.ViewHolder viewHolder1 = (MyListCursorAdapter.ViewHolder) viewHolder;
shareContentDao.flow();
shareContentDao.excludeId = viewHolder1._id;
Snackbar.make(((MyListCursorAdapter.ViewHolder) viewHolder).mTextView1, R.string.deleted_record, Snackbar.LENGTH_LONG)
.setAction(R.string.undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
shareContentDao.excludeId = -1;
mAdapter.changeCursor(cursor = shareContentDao.preLoad(), 0);
}
}).show();
mAdapter.changeCursor(cursor = shareContentDao.preLoad(), 0);
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// We only want the active item to change
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Let the view holder know that this item is being moved or dragged
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}
super.onSelectedChanged(viewHolder, actionState);
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);

viewHolder.itemView.setAlpha(ALPHA_FULL);

if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Tell the view holder it's time to restore the idle state
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(mRecyclerView);


// Intent intent = new Intent();
// intent.setClassName("com.android.settings", "com.android.settings.Settings$UsageAccessSettingsActivity");
// intent.setAction("android.intent.action.MAIN");
// intent.addCategory("android.intent.category.DEFAULT");
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
// | Intent.FLAG_ACTIVITY_CLEAR_TASK
// | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
// startActivity(intent);
}

@Override
Expand All @@ -80,17 +139,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_enable_clipboard) {
mPreferences.edit().putBoolean(KEY_CLIPBOARD, true).commit();
mPreferences.edit().putBoolean(KEY_CLIPBOARD, true).apply();
invalidateOptionsMenu();
return true;
} else if (id == R.id.action_disable_clipboard) {
mPreferences.edit().putBoolean(KEY_CLIPBOARD, false).commit();
mPreferences.edit().putBoolean(KEY_CLIPBOARD, false).apply();
invalidateOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onPause() {
super.onPause();
shareContentDao.flow();
}

@Override
public void invalidateOptionsMenu() {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
public class ShareContentDao {
public SQLiteDatabase writableDatabase;
private final ShareContentDbHelper blockListDbHelper;
public int excludeId;

public ShareContentDao(Context context) {
new ShareContentDbHelper(context);
blockListDbHelper = new ShareContentDbHelper(context);
writableDatabase = blockListDbHelper.getWritableDatabase();
}

public long insert(ShareData data) {
writableDatabase = blockListDbHelper.getWritableDatabase();
if (!writableDatabase.isOpen())
writableDatabase = blockListDbHelper.getWritableDatabase();
writableDatabase.beginTransaction();
ContentValues contentValues = new ContentValues();
contentValues.put("content", data.content);
Expand Down Expand Up @@ -48,18 +51,20 @@ public long insert(ShareData data) {
* @return
*/
public Cursor preLoad() {
writableDatabase = blockListDbHelper.getWritableDatabase();
if (!writableDatabase.isOpen())
writableDatabase = blockListDbHelper.getWritableDatabase();
return writableDatabase.query(ShareContentDbHelper.TABLE_NAME_ITEM,
new String[]{"item._id", "item.content", "item.package_name"},
null,
null,
null,
null,
"item._id",
excludeId == -1 ? null : String.format("item._id != %s", excludeId),
null);
}

public Cursor queryUrlByItemId(int id) {
writableDatabase = blockListDbHelper.getWritableDatabase();
if (!writableDatabase.isOpen())
writableDatabase = blockListDbHelper.getWritableDatabase();
return writableDatabase.query(ShareContentDbHelper.TABLE_NAME_URL_DATA,
new String[]{"url_data.share_url"},
"url_data.item_id=?",
Expand All @@ -68,4 +73,20 @@ public Cursor queryUrlByItemId(int id) {
null,
null);
}

public void flow() {
if (!writableDatabase.isOpen())
writableDatabase = blockListDbHelper.getWritableDatabase();
writableDatabase.beginTransaction();
try {
writableDatabase.delete(ShareContentDbHelper.TABLE_NAME_ITEM, "item._id == ?", new String[]{String.valueOf(excludeId)});
writableDatabase.delete(ShareContentDbHelper.TABLE_NAME_URL_DATA, "url_data.item_id == ?", new String[]{String.valueOf(excludeId)});
writableDatabase.setTransactionSuccessful();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
writableDatabase.endTransaction();
}
excludeId = -1;
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
<string name="save">保存</string>
<string name="cancel">取消</string>
<string name="open_url_in_browser">在浏览器中打开链接</string>
<string name="undo">撤销</string>
<string name="deleted_record">已删除一条记录</string>
</resources>

0 comments on commit da6eb4a

Please sign in to comment.