-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6e092a
commit 72dfa81
Showing
27 changed files
with
977 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
dependencies { | ||
compile 'com.android.support:recyclerview-v7:23.4.0' | ||
compile 'com.squareup.picasso:picasso:2.5.2' | ||
} | ||
|
||
android { | ||
compileSdkVersion 23 | ||
buildToolsVersion "23.0.2" | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
targetSdkVersion 23 | ||
applicationId "com.commonsware.android.kbmouse.context" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0"?> | ||
<manifest package="com.commonsware.android.kbmouse.hotkeys" | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
|
||
<supports-screens | ||
android:anyDensity="true" | ||
android:largeScreens="true" | ||
android:normalScreens="true" | ||
android:smallScreens="true" /> | ||
|
||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
|
||
<application | ||
android:icon="@drawable/ic_launcher" | ||
android:label="@string/app_name" | ||
android:theme="@style/Theme.Apptheme"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
163 changes: 163 additions & 0 deletions
163
...ntext/app/src/main/java/com/commonsware/android/kbmouse/hotkeys/ChoiceCapableAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/*** | ||
Copyright (c) 2015 CommonsWare, LLC | ||
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. | ||
From _The Busy Coder's Guide to Android Development_ | ||
https://commonsware.com/Android | ||
*/ | ||
|
||
package com.commonsware.android.kbmouse.hotkeys; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.KeyEvent; | ||
import android.view.View; | ||
|
||
abstract public class | ||
ChoiceCapableAdapter<T extends RecyclerView.ViewHolder> | ||
extends RecyclerView.Adapter<T> { | ||
private static final long KEY_TIME_DELTA=250; | ||
private final ChoiceMode choiceMode; | ||
private final RecyclerView rv; | ||
private long lastDownKeyTime=-1L; | ||
private long lastUpKeyTime=-1L; | ||
|
||
public ChoiceCapableAdapter(RecyclerView rv, | ||
ChoiceMode choiceMode) { | ||
super(); | ||
this.rv=rv; | ||
this.choiceMode=choiceMode; | ||
} | ||
|
||
void onChecked(int position, boolean isChecked) { | ||
onChecked(position, isChecked, false); | ||
} | ||
|
||
void onChecked(int position, boolean isChecked, boolean updateUI) { | ||
if (choiceMode.isSingleChoice()) { | ||
int checked=choiceMode.getCheckedPosition(); | ||
|
||
if (checked>=0) { | ||
RowController row= | ||
(RowController)rv.findViewHolderForAdapterPosition(checked); | ||
|
||
if (row!=null) { | ||
row.setChecked(false); | ||
} | ||
} | ||
} | ||
|
||
choiceMode.setChecked(position, isChecked); | ||
|
||
if (updateUI) { | ||
notifyItemChanged(position); | ||
rv.scrollToPosition(position); | ||
} | ||
} | ||
|
||
boolean isChecked(int position) { | ||
return(choiceMode.isChecked(position)); | ||
} | ||
|
||
int getCheckedPosition() { | ||
if (choiceMode.isSingleChoice()) { | ||
return(choiceMode.getCheckedPosition()); | ||
} | ||
|
||
throw new IllegalStateException("Can only get checked position for single-choice"); | ||
} | ||
|
||
void onSaveInstanceState(Bundle state) { | ||
choiceMode.onSaveInstanceState(state); | ||
} | ||
|
||
void onRestoreInstanceState(Bundle state) { | ||
choiceMode.onRestoreInstanceState(state); | ||
} | ||
|
||
@Override | ||
public void onViewAttachedToWindow(T holder) { | ||
super.onViewAttachedToWindow(holder); | ||
|
||
if (holder.getAdapterPosition()!=choiceMode.getCheckedPosition()) { | ||
((RowController)holder).setChecked(false); | ||
} | ||
} | ||
|
||
// inspired by http://stackoverflow.com/a/28838834/115145 | ||
|
||
@Override | ||
public void onAttachedToRecyclerView(RecyclerView rv) { | ||
super.onAttachedToRecyclerView(rv); | ||
|
||
if (choiceMode.isSingleChoice()) { | ||
rv.setOnKeyListener(new View.OnKeyListener() { | ||
@Override | ||
public boolean onKey(View v, int keyCode, KeyEvent event) { | ||
if (event.getAction()==KeyEvent.ACTION_DOWN) { | ||
switch (keyCode) { | ||
case KeyEvent.KEYCODE_DPAD_DOWN: | ||
return(chooseNext()); | ||
case KeyEvent.KEYCODE_DPAD_UP: | ||
return(choosePrevious()); | ||
} | ||
} | ||
|
||
return(false); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private boolean chooseNext() { | ||
long now=System.currentTimeMillis(); | ||
boolean result=false; | ||
|
||
if (lastDownKeyTime==-1 || now-lastDownKeyTime>KEY_TIME_DELTA) { | ||
lastDownKeyTime=now; | ||
lastUpKeyTime=-1L; | ||
|
||
int checked=choiceMode.getCheckedPosition(); | ||
|
||
if (checked<0) { | ||
onChecked(0, true, true); | ||
result=true; | ||
} | ||
else if (checked<getItemCount()-1) { | ||
onChecked(checked+1, true, true); | ||
result=true; | ||
} | ||
} | ||
|
||
return(result); | ||
} | ||
|
||
private boolean choosePrevious() { | ||
long now=System.currentTimeMillis(); | ||
boolean result=false; | ||
|
||
if (lastUpKeyTime==-1 || now-lastUpKeyTime>KEY_TIME_DELTA) { | ||
lastUpKeyTime=now; | ||
lastDownKeyTime=-1L; | ||
|
||
int checked=choiceMode.getCheckedPosition(); | ||
|
||
if (checked>0) { | ||
onChecked(checked-1, true, true); | ||
result=true; | ||
} | ||
else if (checked<0) { | ||
onChecked(0, true, true); | ||
result=true; | ||
} | ||
} | ||
|
||
return(result); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
KBMouse/Context/app/src/main/java/com/commonsware/android/kbmouse/hotkeys/ChoiceMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/*** | ||
Copyright (c) 2015 CommonsWare, LLC | ||
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. | ||
From _The Busy Coder's Guide to Android Development_ | ||
https://commonsware.com/Android | ||
*/ | ||
|
||
package com.commonsware.android.kbmouse.hotkeys; | ||
|
||
import android.os.Bundle; | ||
|
||
public interface ChoiceMode { | ||
boolean isSingleChoice(); | ||
int getCheckedPosition(); | ||
void setChecked(int position, boolean isChecked); | ||
boolean isChecked(int position); | ||
void onSaveInstanceState(Bundle state); | ||
void onRestoreInstanceState(Bundle state); | ||
} |
Oops, something went wrong.