Merge pull request #598 from AnyOfYou/master

Add quick switch from notification.
This commit is contained in:
Max Lv
2016-03-12 08:00:42 +08:00
9 changed files with 140 additions and 0 deletions
@@ -41,6 +41,8 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str
.setSmallIcon(R.drawable.ic_stat_shadowsocks)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, service.getString(R.string.stop),
PendingIntent.getBroadcast(service, 0, new Intent(Action.CLOSE), 0))
.addAction(android.R.drawable.ic_menu_manage, service.getString(R.string.quick_switch),
PendingIntent.getActivity(service, 0, new Intent(Action.QUICK_SWITCH), 0))
private lazy val style = new BigTextStyle(builder)
private val showOnUnlock = visible && Utils.isLollipopOrAbove
private var isVisible = true
@@ -0,0 +1,78 @@
package com.github.shadowsocks
import android.content.res.Resources
import android.os.{Handler, Build, Bundle}
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.{Toolbar, DefaultItemAnimator, LinearLayoutManager, RecyclerView}
import android.view.{LayoutInflater, View, ViewGroup}
import android.widget.CheckedTextView
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.utils.Utils
/**
* Created by Lucas on 3/10/16.
*/
class ShadowsocksQuickSwitchActivity extends AppCompatActivity {
private class ProfileViewHolder(val view: View) extends RecyclerView.ViewHolder(view) with View.OnClickListener {
{
val typedArray = obtainStyledAttributes(Array(android.R.attr.selectableItemBackground))
view.setBackgroundResource(typedArray.getResourceId(0, 0))
typedArray.recycle
}
private var item: Profile = _
private val text = itemView.findViewById(android.R.id.text1).asInstanceOf[CheckedTextView]
itemView.setOnClickListener(this)
def bind(item: Profile) {
this.item = item
text.setText(item.name)
if (item.id == ShadowsocksApplication.profileId) {
text.setChecked(true)
} else {
text.setChecked(false)
}
}
def onClick(v: View) {
Utils.stopSsService(ShadowsocksQuickSwitchActivity.this)
ShadowsocksApplication.switchProfile(item.id)
val handler = new Handler(ShadowsocksQuickSwitchActivity.this.getMainLooper)
handler.postDelayed(() => {
Utils.startSsService(ShadowsocksQuickSwitchActivity.this)
}, 3000)
finish
}
}
private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] {
val profiles = ShadowsocksApplication.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
def getItemCount = profiles.length
def onBindViewHolder(vh: ProfileViewHolder, i: Int) = i match {
case _ => vh.bind(profiles(i))
}
private val name = "select_dialog_singlechoice_" + (if (Build.VERSION.SDK_INT >= 21) "material" else "holo")
def onCreateViewHolder(vg: ViewGroup, i: Int) = new ProfileViewHolder(LayoutInflater.from(vg.getContext)
.inflate(Resources.getSystem.getIdentifier(name, "layout", "android"), vg, false))
}
private val profilesAdapter = new ProfilesAdapter
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_quick_switch)
val toolbar = findViewById(R.id.toolbar).asInstanceOf[Toolbar]
toolbar.setTitle(R.string.quick_switch)
val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView]
val lm = new LinearLayoutManager(this)
profilesList.setLayoutManager(lm)
profilesList.setItemAnimator(new DefaultItemAnimator)
profilesList.setAdapter(profilesAdapter)
}
}
@@ -104,6 +104,7 @@ object State {
object Action {
val SERVICE = "com.github.shadowsocks.SERVICE"
val CLOSE = "com.github.shadowsocks.CLOSE"
val QUICK_SWITCH = "com.github.shadowsocks.QUICK_SWITCH"
}
object Route {